Skip to content

Commit 71bf908

Browse files
netheril96QZHelen
authored andcommitted
Use Jetpack API to create DC
Also cleanup explicit dependency on GMSCore, now that we have fully migrated to Jetpack
1 parent 67f5dac commit 71bf908

4 files changed

Lines changed: 22 additions & 38 deletions

File tree

app/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ dependencies {
5353
implementation(libs.androidx.registry.provider)
5454
implementation(libs.androidx.credentials)
5555
implementation(libs.androidx.credentials.play)
56-
implementation(libs.play.services.identity.credentials)
5756
implementation(libs.androidx.registry.provider.play.services)
5857
implementation(libs.lifecycle.runtime.ktx)
5958
implementation(libs.androidx.core.ktx)

app/src/main/java/com/credman/cmwallet/MainActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.credman.cmwallet
22

3-
import android.R.attr.data
43
import android.app.PendingIntent
5-
import android.content.Intent
6-
import android.os.Build
74
import android.os.Bundle
85
import android.util.Log
96
import android.widget.Toast
@@ -15,7 +12,6 @@ import androidx.activity.result.contract.ActivityResultContracts
1512
import com.credman.cmwallet.CmWalletApplication.Companion.TAG
1613
import com.credman.cmwallet.ui.HomeScreen
1714
import com.credman.cmwallet.ui.theme.CMWalletTheme
18-
import com.google.android.gms.identitycredentials.IntentHelper.BUNDLE_KEY_PROVIDER_DATA
1915

2016

2117
class MainActivity : ComponentActivity() {

app/src/main/java/com/credman/cmwallet/ui/HomeViewModel.kt

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
package com.credman.cmwallet.ui
22

3-
import android.content.Context
4-
import android.os.Bundle
5-
import android.util.Base64
63
import android.util.Log
7-
import androidx.credentials.DigitalCredential
4+
import androidx.credentials.CreateDigitalCredentialRequest
5+
import androidx.credentials.CreateDigitalCredentialResponse
6+
import androidx.credentials.CredentialManager
87
import androidx.credentials.ExperimentalDigitalCredentialApi
98
import androidx.lifecycle.ViewModel
109
import androidx.lifecycle.viewModelScope
1110
import com.credman.cmwallet.CmWalletApplication
1211
import com.credman.cmwallet.CmWalletApplication.Companion.TAG
1312
import com.credman.cmwallet.MainActivity
1413
import com.credman.cmwallet.data.model.CredentialItem
15-
import com.credman.cmwallet.loadECPrivateKey
16-
import com.credman.cmwallet.openid4vci.OpenId4VCI
17-
import com.credman.cmwallet.openid4vci.data.AuthorizationDetailResponseOpenIdCredential
18-
import com.credman.cmwallet.openid4vci.data.CredentialRequest
19-
import com.credman.cmwallet.openid4vci.data.TokenRequest
20-
import com.google.android.gms.identitycredentials.CreateCredentialRequest
21-
import com.google.android.gms.identitycredentials.IdentityCredentialManager
2214
import kotlinx.coroutines.flow.MutableStateFlow
2315
import kotlinx.coroutines.flow.StateFlow
2416
import kotlinx.coroutines.flow.asStateFlow
2517
import kotlinx.coroutines.flow.update
2618
import kotlinx.coroutines.launch
2719
import org.json.JSONObject
28-
import java.security.KeyFactory
29-
import java.security.interfaces.ECPrivateKey
30-
import java.security.spec.X509EncodedKeySpec
3120

3221
data class HomeScreenUiState(
3322
val credentials: List<CredentialItem>
@@ -57,25 +46,26 @@ class HomeViewModel : ViewModel() {
5746
fun testIssuance(activity: MainActivity) {
5847
Log.i("HomeViewModel", "testIssuance")
5948
val credOfferJson = JSONObject(CmWalletApplication.credentialRepo.openId4VCITestRequestJson)
60-
val requestJson = JSONObject().put("protocol", "openid4vci1.0").put("data", credOfferJson.toString()).toString()
49+
val requestJson =
50+
JSONObject().put("protocol", "openid4vci1.0").put("data", credOfferJson.toString())
51+
.toString()
6152

62-
// Note: Long term this will be wrapped by a simple structured Jetpack API that
63-
// automatically handles the request Bundle formation and pending intent launching
64-
IdentityCredentialManager.getClient(activity).createCredential(
65-
CreateCredentialRequest(
66-
type = DigitalCredential.TYPE_DIGITAL_CREDENTIAL,
67-
credentialData = Bundle().apply { putString("androidx.credentials.BUNDLE_KEY_REQUEST_JSON", requestJson) },
68-
candidateQueryData = Bundle.EMPTY,
69-
origin = null,
70-
requestJson = requestJson,
71-
resultReceiver = null
72-
)
73-
).addOnSuccessListener {
74-
Log.d(TAG, "Issuance pending intent received ${it.pendingIntent}")
75-
assert(it.pendingIntent != null) { "PendingIntent must be non-null in this case" }
76-
activity.launchIssuanceIntent(it.pendingIntent!!)
77-
}.addOnFailureListener {
78-
Log.e(TAG, "Issuance failure", it)
53+
viewModelScope.launch {
54+
val response = try {
55+
CredentialManager.create(activity).createCredential(
56+
activity,
57+
CreateDigitalCredentialRequest(
58+
origin = null,
59+
requestJson = requestJson,
60+
)
61+
)
62+
} catch (e: Exception) {
63+
Log.e(TAG, "Issuance failure", e)
64+
null
65+
}
66+
(response as? CreateDigitalCredentialResponse)?.let {
67+
Log.d(TAG, "Issuance response ${it.responseJson}")
68+
}
7969
}
8070

8171
// val openId4VCI = OpenId4VCI(requestJson)

gradle/libs.versions.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx
5353
lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx" }
5454
androidx-lifecycle-runtime-compose-android = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose-android", version.ref = "lifecycleRuntimeComposeAndroid" }
5555
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomRuntime" }
56-
play-services-identity-credentials = {module = "com.google.android.gms:play-services-identity-credentials", version = "16.0.0-alpha06"}
5756

5857
[plugins]
5958
android-application = { id = "com.android.application", version.ref = "agp" }

0 commit comments

Comments
 (0)