Files
MosisService/src/main/java/com/omixlab/mosis/NativeService.kt
omigamedev 8cf24d8c2a fix files directory handling: use internal storage and add exception handling
- Use internal files dir instead of external (fixes scoped storage permissions)
- Pass files directory from Android context via JNI instead of hardcoding
- Add exception handling in ScanAppsDirectory to prevent crashes on permission errors
- Use std::error_code overload of fs::exists() to avoid throwing on access denial

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 21:49:58 +01:00

60 lines
2.2 KiB
Kotlin

package com.omixlab.mosis
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.res.AssetManager
import android.os.IBinder
import androidx.core.app.NotificationCompat
class NativeService : Service() {
companion object {
const val NOTIFICATION_ID = 1
const val CHANNEL_ID = "MosisServiceChannel"
init {
System.loadLibrary("mosis-service")
}
}
external fun getBinderNative(): IBinder
external fun setAssetManager(assetManager: AssetManager)
external fun setFilesDir(filesDir: String)
override fun onBind(intent: Intent): IBinder {
return getBinderNative()
}
override fun onCreate() {
super.onCreate()
setAssetManager(assets)
// Use internal files dir - more reliable access in service processes
// For debugging, use: adb shell run-as com.omixlab.mosis ls files/
setFilesDir(filesDir.absolutePath)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()
// Create the notification required for the foreground service
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Mosis Service")
.setContentText("Service is running in the background")
.setSmallIcon(android.R.drawable.ic_dialog_info) // Replace with your app's icon
.setPriority(NotificationCompat.PRIORITY_LOW)
.build()
// Promotes the service to a foreground service
startForeground(NOTIFICATION_ID, notification)
// START_STICKY ensures the service restarts if the system kills it
return START_STICKY
}
private fun createNotificationChannel() {
val serviceChannel = NotificationChannel(
CHANNEL_ID,
"Mosis Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(serviceChannel)
}
}