package de.krisenvorrat.app.data.repository

import android.content.Context
import android.content.Intent
import androidx.core.content.FileProvider
import dagger.hilt.android.qualifiers.ApplicationContext
import de.krisenvorrat.app.domain.usecase.ApkInstaller
import java.io.File
import javax.inject.Inject

internal class ApkInstallerImpl @Inject constructor(
    @ApplicationContext private val context: Context
) : ApkInstaller {

    override fun install(file: File) {
        val uri = FileProvider.getUriForFile(
            context,
            "${context.packageName}.fileprovider",
            file
        )
        val intent = Intent(Intent.ACTION_VIEW).apply {
            setDataAndType(uri, "application/vnd.android.package-archive")
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        context.startActivity(intent)
    }
}
