@@ -8,16 +8,19 @@ import com.google.android.material.snackbar.Snackbar
8
8
import androidx.appcompat.app.AlertDialog
9
9
import androidx.appcompat.app.AppCompatActivity
10
10
import android.util.Log
11
+ import android.widget.TextView
11
12
import com.google.firebase.dynamiclinks.PendingDynamicLinkData
12
13
import com.google.firebase.dynamiclinks.ktx.androidParameters
13
14
import com.google.firebase.dynamiclinks.ktx.dynamicLink
14
15
import com.google.firebase.dynamiclinks.ktx.dynamicLinks
16
+ import com.google.firebase.dynamiclinks.ktx.shortLinkAsync
17
+ import com.google.firebase.dynamiclinks.ktx.component1
18
+ import com.google.firebase.dynamiclinks.ktx.component2
15
19
import com.google.firebase.ktx.Firebase
16
20
import com.google.firebase.quickstart.deeplinks.R
17
21
import com.google.firebase.quickstart.deeplinks.databinding.ActivityMainBinding
18
22
19
23
class MainActivity : AppCompatActivity () {
20
-
21
24
// [START on_create]
22
25
override fun onCreate (savedInstanceState : Bundle ? ) {
23
26
// [START_EXCLUDE]
@@ -35,10 +38,19 @@ class MainActivity : AppCompatActivity() {
35
38
val newDeepLink = buildDeepLink(Uri .parse(DEEP_LINK_URL ), 0 )
36
39
linkSendTextView.text = newDeepLink.toString()
37
40
41
+ // create a short link and display it in the UI
42
+ buildShortLinkFromParams(Uri .parse(DEEP_LINK_URL ), 0 )
43
+
38
44
// Share button click listener
39
45
binding.buttonShare.setOnClickListener { shareDeepLink(newDeepLink.toString()) }
40
46
// [END_EXCLUDE]
41
47
48
+ binding.buttonShareShortLink.setOnClickListener {
49
+ val shortLinkTextView = findViewById<TextView >(R .id.shortLinkViewSend);
50
+ val shortDynamicLink = shortLinkTextView.text;
51
+ shareDeepLink(shortDynamicLink.toString());
52
+ }
53
+
42
54
// [START get_deep_link]
43
55
Firebase .dynamicLinks
44
56
.getDynamicLink(intent)
@@ -106,6 +118,29 @@ class MainActivity : AppCompatActivity() {
106
118
return link.uri
107
119
}
108
120
121
+ @VisibleForTesting
122
+ fun buildShortLinkFromParams (deepLink : Uri , minVersion : Int ) {
123
+ val uriPrefix = getString(R .string.dynamic_links_uri_prefix)
124
+
125
+ // Set dynamic link parameters:
126
+ // * URI prefix (required)
127
+ // * Android Parameters (required)
128
+ // * Deep link
129
+ Firebase .dynamicLinks.shortLinkAsync {
130
+ link = deepLink
131
+ domainUriPrefix = uriPrefix
132
+ androidParameters {
133
+ minimumVersion = minVersion
134
+ }
135
+ }.addOnSuccessListener { (shortLink, flowchartLink) ->
136
+ val shortLinkTextView = findViewById<TextView >(R .id.shortLinkViewSend);
137
+ shortLinkTextView.text = shortLink.toString();
138
+ }.addOnFailureListener(this ) { e ->
139
+ Log .e(TAG , e.toString());
140
+ throw java.lang.Error (e.toString());
141
+ }
142
+ }
143
+
109
144
private fun shareDeepLink (deepLink : String ) {
110
145
val intent = Intent (Intent .ACTION_SEND )
111
146
intent.type = " text/plain"
0 commit comments