Skip to content

Commit

Permalink
Add support for DynamicLinkData on app invite links. (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaVinci9196 authored Nov 16, 2024
1 parent 187910b commit 35ab8af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions firebase-dynamic-links/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
// api project(':firebase-common-ktx')
// api project(':firebase-components')
api 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
annotationProcessor project(':safe-parcel-processor')
}

apply from: '../gradle/publish-android.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import android.os.Parcel;
import androidx.annotation.NonNull;
import com.google.android.gms.common.internal.safeparcel.AbstractSafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelableCreatorAndWriter;

import android.os.Bundle;
import android.net.Uri;
import org.microg.gms.utils.ToStringHelper;

@SafeParcelable.Class
public class DynamicLinkData extends AbstractSafeParcelable {
@Field(1)
public final String dynamicLink;
Expand All @@ -35,6 +37,7 @@ public class DynamicLinkData extends AbstractSafeParcelable {
@Field(6)
public final Uri redirectUrl;

@Constructor
public DynamicLinkData(@Param(1) String dynamicLink, @Param(2) String deepLink, @Param(3) int minVersion, @Param(4) long clickTimestamp, @Param(5) Bundle extensionBundle, @Param(6) Uri redirectUrl) {
this.dynamicLink = dynamicLink;
this.deepLink = deepLink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import androidx.core.view.setPadding
import androidx.lifecycle.lifecycleScope
import com.android.volley.*
import com.android.volley.toolbox.Volley
import com.google.android.gms.common.internal.safeparcel.SafeParcelableSerializer
import com.google.firebase.dynamiclinks.internal.DynamicLinkData
import com.squareup.wire.Message
import com.squareup.wire.ProtoAdapter
import kotlinx.coroutines.CompletableDeferred
import okio.ByteString.Companion.decodeHex
import org.microg.gms.appinvite.*
import org.microg.gms.common.Constants
import org.microg.gms.utils.singleInstanceOf
import org.microg.gms.utils.toBase64
import java.util.*

private const val TAG = "AppInviteActivity"
Expand All @@ -37,6 +38,7 @@ private const val APPINVITE_DEEP_LINK = "com.google.android.gms.appinvite.DEEP_L
private const val APPINVITE_INVITATION_ID = "com.google.android.gms.appinvite.INVITATION_ID"
private const val APPINVITE_OPENED_FROM_PLAY_STORE = "com.google.android.gms.appinvite.OPENED_FROM_PLAY_STORE"
private const val APPINVITE_REFERRAL_BUNDLE = "com.google.android.gms.appinvite.REFERRAL_BUNDLE"
private const val DYNAMIC_LINK_DATA = "com.google.firebase.dynamiclinks.DYNAMIC_LINK_DATA"

class AppInviteActivity : AppCompatActivity() {
private val queue by lazy { singleInstanceOf { Volley.newRequestQueue(applicationContext) } }
Expand Down Expand Up @@ -71,6 +73,8 @@ class AppInviteActivity : AppCompatActivity() {
}

private fun open(appInviteLink: MutateAppInviteLinkResponse) {
val dynamicLinkData = DynamicLinkData(appInviteLink.metadata?.info?.url, appInviteLink.data_?.intentData,
(appInviteLink.data_?.app?.minAppVersion ?: 0).toInt(), System.currentTimeMillis(), null, null)
val intent = Intent(Intent.ACTION_VIEW).apply {
addCategory(Intent.CATEGORY_DEFAULT)
data = appInviteLink.data_?.intentData?.let { Uri.parse(it) }
Expand All @@ -83,17 +87,31 @@ class AppInviteActivity : AppCompatActivity() {
APPINVITE_OPENED_FROM_PLAY_STORE to false
)
)
putExtra(DYNAMIC_LINK_DATA, SafeParcelableSerializer.serializeToBytes(dynamicLinkData))
}
val fallbackIntent = Intent(Intent.ACTION_VIEW).apply {
addCategory(Intent.CATEGORY_DEFAULT)
data = appInviteLink.data_?.fallbackUrl?.let { Uri.parse(it) }
}
val installedVersionCode = runCatching {
intent.resolveActivity(packageManager)?.let {
PackageInfoCompat.getLongVersionCode(packageManager.getPackageInfo(it.packageName, 0))
if (appInviteLink.data_?.packageName != null) {
PackageInfoCompat.getLongVersionCode(packageManager.getPackageInfo(appInviteLink.data_.packageName, 0))
} else {
null
}
}.getOrNull()
if (installedVersionCode != null && (appInviteLink.data_?.app?.minAppVersion == null || installedVersionCode >= appInviteLink.data_.app.minAppVersion)) {
val componentName = intent.resolveActivity(packageManager)
if (componentName == null) {
Log.w(TAG, "open resolve activity is null")
if (appInviteLink.data_?.packageName != null) {
val intentLaunch =
packageManager.getLaunchIntentForPackage(appInviteLink.data_.packageName)
if (intentLaunch != null) {
intent.setComponent(intentLaunch.component)
}
}
}
startActivity(intent)
finish()
} else {
Expand Down

0 comments on commit 35ab8af

Please sign in to comment.