-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
MainActivity.kt
133 lines (114 loc) · 5 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.google.firebase.quickstart.deeplinks.kotlin
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.annotation.VisibleForTesting
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import com.google.firebase.dynamiclinks.ktx.androidParameters
import com.google.firebase.dynamiclinks.ktx.dynamicLink
import com.google.firebase.dynamiclinks.ktx.dynamicLinks
import com.google.firebase.ktx.Firebase
import com.google.firebase.quickstart.deeplinks.R
import com.google.firebase.quickstart.deeplinks.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// [START on_create]
override fun onCreate(savedInstanceState: Bundle?) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val linkSendTextView = binding.linkViewSend
val linkReceiveTextView = binding.linkViewReceive
// Validate that the developer has set the app code.
validateAppCode()
// Create a deep link and display it in the UI
val newDeepLink = buildDeepLink(Uri.parse(DEEP_LINK_URL), 0)
linkSendTextView.text = newDeepLink.toString()
// Share button click listener
binding.buttonShare.setOnClickListener { shareDeepLink(newDeepLink.toString()) }
// [END_EXCLUDE]
// [START get_deep_link]
Firebase.dynamicLinks
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData ->
// Get deep link from result (may be null if no link is found)
var deepLink: Uri? = null
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.link
}
// Handle the deep link. For example, open the linked
// content, or apply promotional credit to the user's
// account.
// ...
// [START_EXCLUDE]
// Display deep link in the UI
if (deepLink != null) {
Snackbar.make(findViewById(android.R.id.content),
"Found deep link!", Snackbar.LENGTH_LONG).show()
linkReceiveTextView.text = deepLink.toString()
} else {
Log.d(TAG, "getDynamicLink: no link found")
}
// [END_EXCLUDE]
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
// [END get_deep_link]
}
// [END on_create]
/**
* Build a Firebase Dynamic Link.
* https://firebase.google.com/docs/dynamic-links/android/create#create-a-dynamic-link-from-parameters
*
* @param deepLink the deep link your app will open. This link must be a valid URL and use the
* HTTP or HTTPS scheme.
* @param minVersion the `versionCode` of the minimum version of your app that can open
* the deep link. If the installed app is an older version, the user is taken
* to the Play store to upgrade the app. Pass 0 if you do not
* require a minimum version.
* @return a [Uri] representing a properly formed deep link.
*/
@VisibleForTesting
fun buildDeepLink(deepLink: Uri, minVersion: Int): Uri {
val uriPrefix = getString(R.string.dynamic_links_uri_prefix)
// Set dynamic link parameters:
// * URI prefix (required)
// * Android Parameters (required)
// * Deep link
// [START build_dynamic_link]
// Build the dynamic link
val link = Firebase.dynamicLinks.dynamicLink {
domainUriPrefix = uriPrefix
androidParameters {
minimumVersion = minVersion
}
link = deepLink
}
// [END build_dynamic_link]
// Return the dynamic link as a URI
return link.uri
}
private fun shareDeepLink(deepLink: String) {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_SUBJECT, "Firebase Deep Link")
intent.putExtra(Intent.EXTRA_TEXT, deepLink)
startActivity(intent)
}
private fun validateAppCode() {
val uriPrefix = getString(R.string.dynamic_links_uri_prefix)
if (uriPrefix.contains("YOUR_APP")) {
AlertDialog.Builder(this)
.setTitle("Invalid Configuration")
.setMessage("Please set your Dynamic Links domain in app/build.gradle")
.setPositiveButton(android.R.string.ok, null)
.create().show()
}
}
companion object {
private const val TAG = "MainActivity"
private const val DEEP_LINK_URL = "https://kotlin.example.com/deeplinks"
}
}