package com.metaads import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.Promise import com.facebook.react.bridge.WritableMap import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableArray import com.facebook.ads.AdSettings import com.facebook.ads.AudienceNetworkAds import android.util.Log import android.content.pm.ApplicationInfo class AdSettings(reactContext: ReactApplicationContext) : NativeAdSettingsSpec(reactContext) { init { // Set debug mode based on build type val isDebug = reactApplicationContext.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 val mode = if (isDebug) { AdSettings.IntegrationErrorMode.INTEGRATION_ERROR_CRASH_DEBUG_MODE } else { AdSettings.IntegrationErrorMode.INTEGRATION_ERROR_CALLBACK_MODE } AdSettings.setIntegrationErrorMode(mode) Log.d("AdSettings", "AdSettings Integration Error Mode set successfully") } companion object { const val NAME = "AdSettings" } override fun getName() = NAME override fun initialize(promise: Promise) { try { if (AudienceNetworkAds.isInitialized(reactApplicationContext)) { Log.w("AdSettings", "Audience Network SDK is already initialized") val result = Arguments.createMap().apply { putBoolean("success", true) putString("message", "Audience Network SDK is already initialized") } promise.resolve(result) return } AudienceNetworkAds .buildInitSettings(reactApplicationContext) .withInitListener(object : AudienceNetworkAds.InitListener { override fun onInitialized(result: AudienceNetworkAds.InitResult) { val map = Arguments.createMap().apply { putBoolean("success", true) putString("message", result.message) } promise.resolve(map) } }) .initialize() } catch (e: Exception) { val response = Arguments.createMap().apply { putBoolean("success", false) putString("message", e.message ?: "Initialization failed") } promise.resolve(response) } } override fun isInitialized(): Boolean { return AudienceNetworkAds.isInitialized(reactApplicationContext) } override fun addTestDevice(deviceHash: String) { try { AdSettings.addTestDevice(deviceHash) Log.d("AdSettings", "Test device added: $deviceHash") } catch (e: Exception) { Log.e("AdSettings", "Error adding test device: "+e.message) } } override fun clearTestDevices() { try { AdSettings.clearTestDevices() Log.d("AdSettings", "All test devices cleared") } catch (e: Exception) { Log.e("AdSettings", "Error clearing test devices: "+e.message) } } override fun getCurrentDeviceHash(): String? { val sp = reactApplicationContext.getSharedPreferences("FBAdPrefs", 0) return sp.getString("deviceIdHash", null) } override fun setDataProcessingOptions(options: ReadableArray, country: Double?, state: Double?) { try { val optionsList = mutableListOf() for (i in 0 until options.size()) { val value = options.getString(i) if (!value.isNullOrBlank()) { optionsList.add(value) } } if (country != null && state != null) { AdSettings.setDataProcessingOptions(optionsList.toTypedArray(), country.toInt(), state.toInt()) } else { AdSettings.setDataProcessingOptions(optionsList.toTypedArray()) } Log.d("AdSettings", "Data processing options set: ${optionsList.joinToString(", ")} (country: $country, state: $state)") } catch (e: Exception) { Log.e("AdSettings", "Error setting data processing options", e) } } }