Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions android-holder-tutorial-sample-app/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
63 changes: 63 additions & 0 deletions android-holder-tutorial-sample-app/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
}

android {
namespace = "com.example.holdertutorial"
compileSdk = 36

defaultConfig {
applicationId = "com.example.holdertutorial"
minSdk = 24
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)
implementation("global.mattr.mobilecredential:holder:6.1.2")
implementation("androidx.navigation:navigation-compose:2.9.0")
implementation("com.google.accompanist:accompanist-permissions:0.36.0")
implementation("com.journeyapps:zxing-android-embedded:4.3.0")
}
21 changes: 21 additions & 0 deletions android-holder-tutorial-sample-app/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.holdertutorial

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.holdertutorial", appContext.packageName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HolderTutorial">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.HolderTutorial">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<data android:scheme="mdoc-openid4vp" />
</intent-filter>
</activity>
<activity
android:name="global.mattr.mobilecredential.holder.webcallback.WebCallbackActivity"
tools:ignore="IntentFilterExportedReceiver"
tools:node="merge">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

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

<data
android:host="credentials"
android:scheme="io.mattrlabs.sample.mobilecredentialtutorialholderapp" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package com.example.holdertutorial

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navDeepLink
import com.example.holdertutorial.ui.theme.HolderTutorialTheme
import global.mattr.mobilecredential.holder.dto.MobileCredential
import global.mattr.mobilecredential.holder.MobileCredentialHolder
import global.mattr.mobilecredential.holder.ProximityPresentationSession
import global.mattr.mobilecredential.holder.issuance.CredentialIssuanceConfiguration
import global.mattr.mobilecredential.holder.issuance.dto.DiscoveredCredentialOffer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
// Claim Credential - Step 1.2: Initialize the SDK
lifecycleScope.launch {
try {
// This function initializes storage and performs validations to ensure the SDK is ready for use.
// Application context is derived from the passed context and persisted.
// redirectUri must be registered during initialization and in the manifest file for the credential issuance workflow to be able to redirect the user back to the application.
// autoTrustMobileCredentialIaca is an optional parameter that, when set to true, will automatically trust any IACAs received during the credential issuance flow.
// This is not recommended for production applications, but can be useful for the quickstart.
MobileCredentialHolder.getInstance().initialize(
context = this@MainActivity,
// Step 4.1: Add credential issuance configuration
credentialIssuanceConfiguration = CredentialIssuanceConfiguration(
redirectUri = "io.mattrlabs.sample.mobilecredentialtutorialholderapp:" +
"//credentials/callback",
autoTrustMobileCredentialIaca = true
)
)
} catch (e: Exception) {
Log.e("MainActivity", "SDK initialization failed", e)
}
}

setContent {
HolderTutorialTheme {
val navController = rememberNavController()
Scaffold { innerPadding ->
NavHost(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.padding(8.dp),
startDestination = "home",
navController = navController,
) {
composable("home") {
HomeScreen(this@MainActivity, navController)
}
composable("scanOffer") {
// Claim Credential - Step 2.5: Add "Scan Offer" screen call
ScanOfferScreen(navController)
}
composable("retrievedCredential") {
// Claim Credential - Step 4.9: Add "Retrieved Credential" screen call
RetrievedCredentialsScreen()
}
composable("presentationQr") {
// Proximity Presentation - Step 1.2: Add "QR Presentation" screen call
PresentationQrScreen(this@MainActivity, navController)
}
composable("presentationSelectCredentials") {
// Proximity Presentation - Step 2.6: Add "Select Credential" screen call
PresentationSelectCredentialsScreen(this@MainActivity)
}
// Online Presentation - Step 2.2: Add "Online Presentation" screen call
composable(
"onlinePresentation",
deepLinks = listOf(
navDeepLink { uriPattern = "mdoc-openid4vp://{wildcard}" }
)
) {
@Suppress("DEPRECATION")
val deepLink = it.arguments
?.getParcelable<Intent>(NavController.KEY_DEEP_LINK_INTENT)
?.data
?.toString() ?: ""

OnlinePresentationScreen(this@MainActivity, deepLink)
}
}
}
}
}
}
}

@Composable
fun HomeScreen(activity: Activity, navController: NavController) {
val coroutineScope = rememberCoroutineScope()
var transactionCode by remember { mutableStateOf("") }

Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Button(onClick = { navController.navigate("scanOffer") }, Modifier.fillMaxWidth()) {
Text("Claim Credential")
}

// Proximity Presentation - Step 1.3: Add button for starting the credentials presentation workflow
Button(onClick = { navController.navigate("presentationQr") }, Modifier.fillMaxWidth()) {
Text("Present Credentials")
}

// Claim Credential - Step 3.3: Display discovered credential offer
SharedData.discoveredCredentialOffer?.let { discoveredOffer ->
Text("Received Credential Offer from ${discoveredOffer.issuer}")
LazyColumn(Modifier.fillMaxWidth()) {
items(discoveredOffer.credentials, key = { it.doctype }) { credential ->
Card(Modifier.fillMaxWidth()) {
Column(Modifier.padding(4.dp)) {
Text("Name: ${credential.name ?: ""}")
Text("DocType: ${credential.doctype}")
}
}
}
}

// Claim Credential - Step 4.3: Add transaction code input
if (discoveredOffer.transactionCode != null) {
OutlinedTextField(
value = transactionCode,
onValueChange = { transactionCode = it },
label = { Text("Transaction Code") },
modifier = Modifier.fillMaxWidth()
)
}

// Claim Credential - Step 4.5: Add consent button
Spacer(Modifier.weight(1f))
Button(
onClick = {
onRetrieveCredentials(coroutineScope, activity, navController, transactionCode)
},
Modifier.fillMaxWidth()
) { Text("Consent and retrieve Credential(s)") }
}
}
}

// Claim Credential - Step 4.4: Create function to retrieve credentials
private fun onRetrieveCredentials(
coroutineScope: CoroutineScope,
activity: Activity,
navController: NavController,
transactionCode: String
) {
coroutineScope.launch {
try {
val mdocHolder = MobileCredentialHolder.getInstance()
// Each of the credential retrieval results contains:
// - Credential document type.
// - Either the credential ID, that can be now used to access the credential from the local storage (see code below), OR
// an error if the credential retrieval failed.
val retrieveCredentialResults = mdocHolder.retrieveCredentials(
activity,
SharedData.scannedOffer!!,
clientId = "android-mobile-credential-tutorial-holder-app",
transactionCode = transactionCode
)

// Claim Credential - Step 4.6: Display retrieved credentials
SharedData.retrievedCredentials = retrieveCredentialResults.mapNotNull {
try {
// The credential ID can be used to get the full credential from the SDK storage.
// fetchUpdatedStatusList - Whether to enforce the online revocation status check for the credential.
// Returned object contains all credential data, including the user's PII.
mdocHolder.getCredential(it.credentialId!!, fetchUpdatedStatusList = false)
} catch (e: Exception) {
val msg = "Failed to get credential from storage"
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
null
}
}

navController.navigate("retrievedCredential")
SharedData.discoveredCredentialOffer = null
} catch (e: Exception) {
Toast.makeText(activity, "Failed to retrieve credentials", Toast.LENGTH_SHORT).show()
}
}
}

object SharedData {
// Claim Credential - Step 3.1: Add discovered credential offer variables
var scannedOffer: String? = null
var discoveredCredentialOffer: DiscoveredCredentialOffer? = null
// Claim Credential - Step 4.2: Add retrieved credentials variable
var retrievedCredentials: List<MobileCredential> = emptyList()
// Proximity Presentation - Step 2.1: Add proximity presentation request variable
var proximityPresentationRequest: ProximityPresentationSession.CredentialRequestInfo? = null
}
Loading
Loading