Skip to content

Commit

Permalink
Add AppLink support from Intent Extra
Browse files Browse the repository at this point in the history
Summary: `al_applink_data` can be put in intent's bundle extra and we also need to support that

Differential Revision: D62169805

fbshipit-source-id: d35a7ab1694a73ce16d34a806505efba48cf3494
  • Loading branch information
Zilin Zhang committed Sep 11, 2024
1 parent dc54ece commit 78a7098
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -47,6 +51,11 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() {
.thenReturn(mockSharedPreference)
}

@After
fun after() {
mockSharedPreference.edit().clear()
}

@Test
fun testGetInstance() {
assertNotNull(AppLinkManager.getInstance())
Expand All @@ -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))
Expand Down

0 comments on commit 78a7098

Please sign in to comment.