package com.gh.gamecenter.xapk.pi

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import java.io.File

class NonSplitPackagesInstaller(
    private val file: File
) : IPackageInstaller {

    companion object {
        private const val MIME_TYPE_ANDROID_PACKAGE_ARCHIVE = "application/vnd.android.package-archive"
    }

    override fun install(context: Context) {
        val installIntent = Intent(Intent.ACTION_VIEW)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val uri = FileProvider.getUriForFile(context, context.packageName, file)
            installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            installIntent.setDataAndType(uri, MIME_TYPE_ANDROID_PACKAGE_ARCHIVE)
        } else {
            installIntent.setDataAndType(
                Uri.fromFile(file),
                MIME_TYPE_ANDROID_PACKAGE_ARCHIVE
            )
        }
        context.startActivity(installIntent)
    }
}