package com.agedeclaration import android.app.Activity import com.facebook.react.bridge.* import com.facebook.react.modules.core.DeviceEventManagerModule import com.google.android.gms.tasks.Task import com.google.android.play.core.ageverification.AgeVerificationManager import com.google.android.play.core.ageverification.AgeVerificationManagerFactory import com.google.android.play.core.ageverification.model.AgeVerificationResult class AgeDeclarationModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { private var ageVerificationManager: AgeVerificationManager? = null private var ageThresholds: IntArray = intArrayOf() override fun getName(): String { return "AgeDeclarationModule" } /** * Sets the age range thresholds for the application */ @ReactMethod fun setAgeThresholds(thresholds: ReadableArray, promise: Promise) { try { // Validate thresholds if (thresholds.size() == 0) { promise.reject("INVALID_THRESHOLDS", "At least one threshold is required") return } // Convert to IntArray and validate val thresholdList = mutableListOf() for (i in 0 until thresholds.size()) { val threshold = thresholds.getInt(i) // Validate range (1-18) if (threshold < 1 || threshold > 18) { promise.reject("INVALID_THRESHOLDS", "Thresholds must be between 1 and 18") return } thresholdList.add(threshold) } // Validate ascending order val sorted = thresholdList.sorted() if (thresholdList != sorted) { promise.reject("INVALID_THRESHOLDS", "Thresholds must be in ascending order") return } // Validate 2-year separation for (i in 0 until thresholdList.size - 1) { if (thresholdList[i + 1] - thresholdList[i] < 2) { promise.reject( "INVALID_THRESHOLDS", "Thresholds must have at least 2-year separation" ) return } } this.ageThresholds = thresholdList.toIntArray() // Initialize Age Verification Manager currentActivity?.let { activity -> ageVerificationManager = AgeVerificationManagerFactory.create(activity) } val result = Arguments.createMap() result.putBoolean("success", true) promise.resolve(result) } catch (e: Exception) { promise.reject("ERROR", "Failed to set age thresholds: ${e.message}", e) } } /** * Requests age range information from Google Play */ @ReactMethod fun requestAgeRange(promise: Promise) { try { // Validate thresholds are set if (ageThresholds.isEmpty()) { promise.reject( "THRESHOLDS_NOT_SET", "Age thresholds must be set before requesting age range" ) return } val activity = currentActivity if (activity == null) { promise.reject("NO_ACTIVITY", "No activity available") return } val manager = ageVerificationManager if (manager == null) { promise.reject("NOT_INITIALIZED", "Age verification manager not initialized") return } // Request age verification from Google Play // This requires Google Play Services and the Age Verification API val requestTask: Task = manager.requestAgeVerification() requestTask.addOnSuccessListener { result -> val responseMap = Arguments.createMap() when (result) { AgeVerificationResult.VERIFIED -> { responseMap.putString("status", "verified") responseMap.putString("message", "Age verification successful") } AgeVerificationResult.NOT_VERIFIED -> { responseMap.putString("status", "not_verified") responseMap.putString("message", "User does not meet age requirements") } AgeVerificationResult.UNKNOWN -> { responseMap.putString("status", "unknown") responseMap.putString("message", "Age verification status unknown") } else -> { responseMap.putString("status", "error") responseMap.putString("message", "Unknown verification result") } } val thresholdsArray = Arguments.createArray() ageThresholds.forEach { thresholdsArray.pushInt(it) } responseMap.putArray("thresholds", thresholdsArray) promise.resolve(responseMap) }.addOnFailureListener { exception -> promise.reject("API_ERROR", "Age verification failed: ${exception.message}", exception) } } catch (e: Exception) { promise.reject("ERROR", "Failed to request age range: ${e.message}", e) } } /** * Checks if age range API is supported on this device */ @ReactMethod fun isSupported(promise: Promise) { try { // Age Verification API is available on Android 15+ (API level 35+) // For devices with Google Play Services val isSupported = android.os.Build.VERSION.SDK_INT >= 35 promise.resolve(isSupported) } catch (e: Exception) { promise.reject("ERROR", "Failed to check support: ${e.message}", e) promise.resolve(false) } } }