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 2afd4d8e93..02295c89cd 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 @@ -11,9 +11,11 @@ package com.facebook.appevents.internal import android.app.Activity import android.app.Application import android.content.Context +import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Bundle +import android.util.Log import com.facebook.FacebookSdk import com.facebook.internal.instrument.crashshield.AutoHandleExceptions import org.json.JSONObject @@ -44,16 +46,30 @@ class AppLinkManager private constructor() { fun handleURL(activity: Activity) { val uri = activity.intent.data ?: return - processCampaignIds(uri) + processCampaignIds(uri, activity.intent) } - fun processCampaignIds(uri: Uri) { - val applinkData = uri.getQueryParameter(APPLINK_DATA_KEY) ?: return + fun processCampaignIds(uri: Uri, intent: Intent) { + val campaignIDs = getCampaignIDFromUri(uri) ?: getCampaignIDFromIntentExtra(intent) + if (campaignIDs != null) { + preferences.edit().putString(CAMPAIGN_IDS_KEY, campaignIDs).apply() + } + } + + fun getCampaignIDFromUri(uri: Uri): String? { + val applinkData = uri.getQueryParameter(APPLINK_DATA_KEY) ?: return null try { val json = JSONObject(applinkData) - val campaignIDs = json.getString(CAMPAIGN_IDS_KEY) ?: return - preferences.edit().putString(CAMPAIGN_IDS_KEY, campaignIDs).apply() - } catch (_: Exception) {} + return json.getString(CAMPAIGN_IDS_KEY) + } catch (_: Exception) { + Log.d("AppLinkManager", "Fail to parse Applink data from Uri") + } + return null + } + + fun getCampaignIDFromIntentExtra(intent: Intent): String? { + val applinkBundle = intent.getBundleExtra(APPLINK_DATA_KEY) ?: return null + return applinkBundle.getString(CAMPAIGN_IDS_KEY) } fun getInfo(key: String): String? { 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 70bc87dc08..1f3ea6afc8 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 @@ -13,9 +13,11 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.net.Uri +import android.os.Bundle import com.facebook.FacebookPowerMockTestCase import com.facebook.FacebookSdk import com.facebook.MockSharedPreference +import org.junit.After import org.junit.Before import org.junit.Test import org.mockito.kotlin.any @@ -31,14 +33,16 @@ import kotlin.test.assertNull class AppLinkManagerTest: FacebookPowerMockTestCase() { private lateinit var mockApplicationContext: Context - private lateinit var mockSharedPreference: SharedPreferences private lateinit var mockActivity: Activity + companion object { + private val mockSharedPreference: SharedPreferences = MockSharedPreference() + } + @Before fun init() { mockActivity = mock() mockApplicationContext = mock() - mockSharedPreference = MockSharedPreference() PowerMockito.mockStatic(FacebookSdk::class.java) whenever(FacebookSdk.isInitialized()).thenReturn(true) whenever(FacebookSdk.getApplicationId()).thenReturn("123456789") @@ -47,6 +51,11 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() { .thenReturn(mockSharedPreference) } + @After + fun after() { + mockSharedPreference.edit().clear() + } + @Test fun testGetInstance() { assertNotNull(AppLinkManager.getInstance()) @@ -72,10 +81,23 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() { } @Test - fun testHandleNonnullCampaignIDs() { + fun testHandleNonnullCampaignIDsFromURL() { 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")) + whenever(mockActivity.intent).thenReturn(intent) + AppLinkManager.getInstance()?.handleURL(mockActivity) + + assertEquals("test_campaign_1234", mockSharedPreference.getString("campaign_ids", null)) + } + + @Test + fun testHandleNonnullCampaignIDsFromExtra() { + val intent = Intent() + val bundle = Bundle() + bundle.putString("campaign_ids", "test_campaign_1234") + intent.putExtra("al_applink_data", bundle) + whenever(mockActivity.intent).thenReturn(intent) + intent.setData(Uri.parse("fb123://test.com")) AppLinkManager.getInstance()?.handleURL(mockActivity) assertEquals("test_campaign_1234", mockSharedPreference.getString("campaign_ids", null))