Skip to content

Commit

Permalink
feat: Branch SDK Integration
Browse files Browse the repository at this point in the history
Fixes: LEARNER-9787
  • Loading branch information
HamzaIsrar12 committed Feb 26, 2024
1 parent b543768 commit 047f404
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 4 deletions.
36 changes: 35 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ android {
productFlavors {
prod {
dimension 'env'
setupBranchConfigFields(it)
}
develop {
dimension 'env'
setupBranchConfigFields(it)
}
stage {
dimension 'env'
setupBranchConfigFields(it)
}
}

Expand Down Expand Up @@ -130,6 +133,11 @@ dependencies {
// Braze SDK Integration
implementation "com.braze:braze-segment-kotlin:1.4.2"

// Branch SDK Integration
implementation 'io.branch.sdk.android:library:5.9.0'
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
implementation "com.android.installreferrer:installreferrer:2.2"

androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
Expand All @@ -139,4 +147,30 @@ dependencies {
testImplementation "io.mockk:mockk-android:$mockk_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation "androidx.arch.core:core-testing:$android_arch_version"
}
}

private def setupBranchConfigFields(buildType) {
def branchConfig = configHelper.fetchConfig().get("BRANCH")
def branchKey = ""
def branchUriScheme = ""
def branchHost = ""
def branchAlternateHost = ""

if (branchConfig && branchConfig.get("ENABLED")) {
branchKey = branchConfig.getOrDefault("KEY", "")
branchUriScheme = branchConfig.getOrDefault("URI_SCHEME", "")
branchHost = branchConfig.getOrDefault("HOST", "")
branchAlternateHost = branchConfig.getOrDefault("ALTERNATE_HOST", "")

// Validation: Throw exception if any field is empty
if (branchKey.isEmpty() || branchUriScheme.isEmpty() || branchHost.isEmpty() ||
branchAlternateHost.isEmpty()) {
throw new IllegalStateException("One or more Branch configuration fields are empty.")
}
}

buildType.resValue "string", "branch_key", branchKey
buildType.resValue "string", "branch_uri_scheme", branchUriScheme
buildType.resValue "string", "branch_host", branchHost
buildType.resValue "string", "branch_alternate_host", branchAlternateHost
}
37 changes: 37 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
</intent>
</queries>

<queries>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="text/plain" />
</intent>
</queries>

<application
android:name=".OpenEdXApp"
android:allowBackup="false"
Expand All @@ -38,6 +45,28 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- Branch URI Scheme -->
<intent-filter>
<data
android:host="open"
android:scheme="@string/branch_uri_scheme" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="https" />
<data android:host="@string/branch_host" />
<data android:host="@string/branch_alternate_host" />
</intent-filter>
</activity>

<provider
Expand All @@ -60,6 +89,14 @@
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="androidx.media3.cast.DefaultCastOptionsProvider" />

<!-- Branch init -->
<meta-data
android:name="io.branch.sdk.BranchKey"
android:value="@string/branch_key" />
<meta-data
android:name="io.branch.sdk.BranchKey.test"
android:value="@string/branch_key" />

<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/org/openedx/app/AppActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.openedx.app

import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -12,6 +14,8 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.fragment.app.Fragment
import androidx.window.layout.WindowMetricsCalculator
import io.branch.referral.Branch
import io.branch.referral.Branch.BranchUniversalReferralInitListener
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.app.databinding.ActivityAppBinding
Expand Down Expand Up @@ -134,6 +138,43 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
}
}

override fun onStart() {
super.onStart()

if (viewModel.isBranchEnabled) {
val callback = BranchUniversalReferralInitListener { _, linkProperties, error ->
if (linkProperties != null) {
Log.i(BRANCH_TAG, "Branch init complete.")
Log.i(BRANCH_TAG, linkProperties.controlParams.toString())
} else if (error != null) {
Log.e(BRANCH_TAG, "Branch init failed. Caused by -" + error.message)
}
}

Branch.sessionBuilder(this)
.withCallback(callback)
.withData(this.intent.data)
.init()
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent

if (viewModel.isBranchEnabled) {
if (intent?.getBooleanExtra(BRANCH_FORCE_NEW_SESSION, false) == true) {
Branch.sessionBuilder(this).withCallback { referringParams, error ->
if (error != null) {
Log.e(BRANCH_TAG, error.message)
} else if (referringParams != null) {
Log.i(BRANCH_TAG, referringParams.toString())
}
}.reInit()
}
}
}

private fun addFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.add(R.id.container, fragment)
Expand Down Expand Up @@ -173,5 +214,7 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {
const val TOP_INSET = "topInset"
const val BOTTOM_INSET = "bottomInset"
const val CUTOUT_INSET = "cutoutInset"
const val BRANCH_TAG = "Branch"
const val BRANCH_FORCE_NEW_SESSION = "branch_force_new_session"
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/org/openedx/app/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class AppViewModel(

private var logoutHandledAt: Long = 0

val isBranchEnabled get() = config.getBranchConfig().enabled

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
setUserId()
Expand All @@ -55,4 +57,4 @@ class AppViewModel(
}
}

}
}
12 changes: 10 additions & 2 deletions app/src/main/java/org/openedx/app/OpenEdXApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import com.google.firebase.FirebaseOptions
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.initialize
import io.branch.referral.Branch
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
Expand All @@ -29,6 +30,13 @@ class OpenEdXApp : Application() {
if (config.getFirebaseConfig().enabled) {
Firebase.initialize(this)
}
}

}
if (config.getBranchConfig().enabled) {
if (BuildConfig.DEBUG) {
Branch.enableTestMode()
Branch.enableLogging()
}
Branch.getAutoInstance(this)
}
}
}
20 changes: 20 additions & 0 deletions core/src/main/java/org/openedx/core/config/BranchConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.openedx.core.config

import com.google.gson.annotations.SerializedName

data class BranchConfig(
@SerializedName("ENABLED")
val enabled: Boolean = false,

@SerializedName("KEY")
val key: String = "",

@SerializedName("URI_SCHEME")
val uriScheme: String = "",

@SerializedName("HOST")
val host: String = "",

@SerializedName("ALTERNATE_HOST")
val alternateHost: String = "",
)
5 changes: 5 additions & 0 deletions core/src/main/java/org/openedx/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class Config(context: Context) {
return getObjectOrNewInstance(PROGRAM, ProgramConfig::class.java)
}

fun getBranchConfig(): BranchConfig {
return getObjectOrNewInstance(BRANCH, BranchConfig::class.java)
}

fun isWhatsNewEnabled(): Boolean {
return getBoolean(WHATS_NEW_ENABLED, false)
}
Expand Down Expand Up @@ -164,6 +168,7 @@ class Config(context: Context) {
private const val PRE_LOGIN_EXPERIENCE_ENABLED = "PRE_LOGIN_EXPERIENCE_ENABLED"
private const val DISCOVERY = "DISCOVERY"
private const val PROGRAM = "PROGRAM"
private const val BRANCH = "BRANCH"
private const val COURSE_NESTED_LIST_ENABLED = "COURSE_NESTED_LIST_ENABLED"
private const val COURSE_BANNER_ENABLED = "COURSE_BANNER_ENABLED"
private const val COURSE_TOP_TAB_BAR_ENABLED = "COURSE_TOP_TAB_BAR_ENABLED"
Expand Down

0 comments on commit 047f404

Please sign in to comment.