In my case,
package you_package_name
import android.content.pm.PackageManager
import android.view.View
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.ReactShadowNode
import com.facebook.react.uimanager.ViewManager
import android.util.Log
import android.os.Build
import android.content.Context
class InstallerCheckerModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext), ReactPackage {
override fun getName(): String {
return "InstallerChecker"
}
// Function to check if the app was installed from Google Play Store
@ReactMethod
fun isInstalledFromGooglePlay(promise: Promise) {
try {
val installerPackageName = getInstallerPackageName(reactApplicationContext, reactApplicationContext.packageName)
Log.d("InstallerChecker", "Installer package name: $installerPackageName")
if (installerPackageName != null && installerPackageName == "com.android.vending") {
promise.resolve(true) // App was installed from Google Play Store
} else {
promise.resolve(false) // App was not installed from Google Play Store
}
} catch (e: Exception) {
promise.reject("ERROR", e.message)
}
}
// Get the installer package name
private fun getInstallerPackageName(context: Context, packageName: String): String? {
return kotlin.runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
context.packageManager.getInstallSourceInfo(packageName).installingPackageName
else
@Suppress("DEPRECATION")
context.packageManager.getInstallerPackageName(packageName)
}.getOrNull()
}
override fun createNativeModules(p0: ReactApplicationContext): MutableList<NativeModule> {
return mutableListOf()
}
override fun createViewManagers(p0: ReactApplicationContext): MutableList<ViewManager<View, ReactShadowNode<*>>> {
return mutableListOf()
}
}
getInstallSourceInfo(String)
? – Tungstite