Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external intent launcher #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions app/src/main/java/com/solana/pay/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class MainActivity : AppCompatActivity() {
val transactionSignatureBase58: String?
)

private val solanaPayActivityResult = registerForActivityResult(object : ActivityResultContract<Uri, SolanaPayResult>() {
// Sends an intent out that launches the internal activity/component "SolanaPayActivity"
private val solanaPayActivityResultInternal = registerForActivityResult(object : ActivityResultContract<Uri, SolanaPayResult>() {
override fun createIntent(context: Context, solanaPayUri: Uri): Intent {
val i = Intent()
i.component = ComponentName(packageName, "com.solana.pay.sample.SolanaPayActivityViaInternal")
Expand All @@ -39,6 +40,21 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
}

// Sends an intent out externally to be received by app's with matching intent filters for `solanaPayUri`
private val solanaPayActivityResultExternal = registerForActivityResult(object : ActivityResultContract<Uri, SolanaPayResult>() {
override fun createIntent(context: Context, solanaPayUri: Uri): Intent {
val i = Intent(Intent.ACTION_VIEW, solanaPayUri)
return i
}

override fun parseResult(resultCode: Int, intent: Intent?): SolanaPayResult {
return SolanaPayResult(resultCode, intent?.getStringExtra(SolanaPayAndroidContract.EXTRA_SIGNATURE))
}
}) { result ->
val str = "Result=${result.result}, sig=${result.transactionSignatureBase58}"
Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -47,7 +63,7 @@ class MainActivity : AppCompatActivity() {

viewBinding.buttonInternalUri.setOnClickListener {
// NOTE: this is an arbitrary address - please don't perform any real transfers to it!
solanaPayActivityResult.launch(Uri.parse("solana:84npKJKZy8ixjdq8UChZULDUea2Twt8ThxjiqKd7QZ54?amount=100&memo=Test%20xfer"))
solanaPayActivityResultExternal.launch(Uri.parse("solana:84npKJKZy8ixjdq8UChZULDUea2Twt8ThxjiqKd7QZ54?amount=100&memo=Test%20xfer"))
}
}
}