diff --git a/facebook-core/src/main/java/com/facebook/appevents/internal/AppLinkManager.kt b/facebook-core/src/main/java/com/facebook/appevents/internal/AppLinkManager.kt index 197f046754..2afd4d8e93 100644 --- a/facebook-core/src/main/java/com/facebook/appevents/internal/AppLinkManager.kt +++ b/facebook-core/src/main/java/com/facebook/appevents/internal/AppLinkManager.kt @@ -12,9 +12,11 @@ import android.app.Activity import android.app.Application import android.content.Context import android.content.SharedPreferences +import android.net.Uri import android.os.Bundle import com.facebook.FacebookSdk import com.facebook.internal.instrument.crashshield.AutoHandleExceptions +import org.json.JSONObject @AutoHandleExceptions class AppLinkManager private constructor() { @@ -25,6 +27,8 @@ class AppLinkManager private constructor() { companion object { const val APPLINK_INFO = "com.facebook.sdk.APPLINK_INFO" + const val APPLINK_DATA_KEY = "al_applink_data" + const val CAMPAIGN_IDS_KEY = "campaign_ids" @Volatile private var instance: AppLinkManager? = null @@ -38,7 +42,19 @@ class AppLinkManager private constructor() { } } - fun handleURL(activity: Activity) {} + fun handleURL(activity: Activity) { + val uri = activity.intent.data ?: return + processCampaignIds(uri) + } + + fun processCampaignIds(uri: Uri) { + val applinkData = uri.getQueryParameter(APPLINK_DATA_KEY) ?: return + try { + val json = JSONObject(applinkData) + val campaignIDs = json.getString(CAMPAIGN_IDS_KEY) ?: return + preferences.edit().putString(CAMPAIGN_IDS_KEY, campaignIDs).apply() + } catch (_: Exception) {} + } fun getInfo(key: String): String? { return preferences.getString(key, null) diff --git a/facebook-core/src/main/java/com/facebook/internal/Utility.kt b/facebook-core/src/main/java/com/facebook/internal/Utility.kt index c2ae21a811..f68583c61e 100644 --- a/facebook-core/src/main/java/com/facebook/internal/Utility.kt +++ b/facebook-core/src/main/java/com/facebook/internal/Utility.kt @@ -35,6 +35,7 @@ import com.facebook.FacebookSdk import com.facebook.GraphRequest import com.facebook.HttpMethod import com.facebook.appevents.UserDataStore +import com.facebook.appevents.internal.AppLinkManager import com.facebook.internal.ProfileInformationCache.getProfileInformation import com.facebook.internal.ProfileInformationCache.putProfileInformation import com.facebook.internal.instrument.crashshield.AutoHandleExceptions @@ -618,7 +619,11 @@ object Utility { params.put("installer_package", attributionIdentifiers.androidInstallerPackage) } } - } /* no op */ + val campaignIDs = AppLinkManager.getInstance()?.getInfo(AppLinkManager.CAMPAIGN_IDS_KEY) + if (campaignIDs != null) { + params.put("campaign_ids", campaignIDs) + } + } /** * Get the app version of the app, as specified by the manifest. diff --git a/facebook-core/src/test/kotlin/com/facebook/appevents/internal/AppLinkManagerTest.kt b/facebook-core/src/test/kotlin/com/facebook/appevents/internal/AppLinkManagerTest.kt index 382ad9ea1b..70bc87dc08 100644 --- a/facebook-core/src/test/kotlin/com/facebook/appevents/internal/AppLinkManagerTest.kt +++ b/facebook-core/src/test/kotlin/com/facebook/appevents/internal/AppLinkManagerTest.kt @@ -8,8 +8,11 @@ package com.facebook.appevents.internal +import android.app.Activity import android.content.Context +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import com.facebook.FacebookPowerMockTestCase import com.facebook.FacebookSdk import com.facebook.MockSharedPreference @@ -20,16 +23,20 @@ import org.mockito.kotlin.mock import org.mockito.kotlin.whenever import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest +import kotlin.test.assertEquals import kotlin.test.assertNotNull +import kotlin.test.assertNull @PrepareForTest(FacebookSdk::class) class AppLinkManagerTest: FacebookPowerMockTestCase() { private lateinit var mockApplicationContext: Context private lateinit var mockSharedPreference: SharedPreferences + private lateinit var mockActivity: Activity @Before fun init() { + mockActivity = mock() mockApplicationContext = mock() mockSharedPreference = MockSharedPreference() PowerMockito.mockStatic(FacebookSdk::class.java) @@ -44,4 +51,33 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() { fun testGetInstance() { assertNotNull(AppLinkManager.getInstance()) } + + @Test + fun testHandleInvalidAppLinkData() { + val intent = Intent() + whenever(mockActivity.intent).thenReturn(intent) + intent.setData(Uri.parse("fb123://test.com?al_applink_data=123")) + AppLinkManager.getInstance()?.handleURL(mockActivity) + + assertNull(mockSharedPreference.getString("campaign_ids", null)) + } + + @Test + fun testHandleNullCampaignIDs() { + val intent = Intent() + whenever(mockActivity.intent).thenReturn(intent) + AppLinkManager.getInstance()?.handleURL(mockActivity) + + assertNull(mockSharedPreference.getString("campaign_ids", null)) + } + + @Test + fun testHandleNonnullCampaignIDs() { + val intent = Intent() + whenever(mockActivity.intent).thenReturn(intent) + intent.setData(Uri.parse("fb123://test.com?al_applink_data=%7B%22acs_token%22%3A+%22test_token_1234567%22%2C+%22campaign_ids%22%3A+%22test_campaign_1234%22%2C+%22advertiser_id%22%3A+%22test_advertiserid_12345%22%7D")) + AppLinkManager.getInstance()?.handleURL(mockActivity) + + assertEquals("test_campaign_1234", mockSharedPreference.getString("campaign_ids", null)) + } }