|
1 | 1 | package id.walt.wallet2.mobile |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import androidx.fragment.app.FragmentActivity |
4 | 5 | import id.walt.wallet2.persistence.encryption.AndroidDatabaseEncryptionKeyProvider |
5 | 6 | import id.walt.wallet2.persistence.keys.AndroidPlatformKeyProvider |
6 | 7 | import id.walt.wallet2.persistence.stores.DriverFactory |
| 8 | +import java.lang.ref.WeakReference |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * Android [MobileWallet] factory backed by Android KeyStore and an app-private SQLDelight database. |
10 | 12 | * |
11 | | - * @param context Android context used to open the wallet database. Protected key use requires this |
12 | | - * context to be an interactive [androidx.fragment.app.FragmentActivity]. |
| 13 | + * The single-context constructor weakly references an interactive [FragmentActivity]. Apps that retain |
| 14 | + * a wallet across activity recreation should use the provider-based constructor. |
13 | 15 | */ |
14 | | -public actual class MobileWalletFactory(private val context: Context) { |
| 16 | +public actual class MobileWalletFactory private constructor( |
| 17 | + private val applicationContext: Context, |
| 18 | + private val interactionContextProvider: () -> FragmentActivity?, |
| 19 | + @Suppress("UNUSED_PARAMETER") marker: Unit, |
| 20 | +) { |
| 21 | + /** Creates an activity-scoped factory while retaining only the application context strongly. */ |
| 22 | + public constructor(context: Context) : this( |
| 23 | + applicationContext = context.applicationContext, |
| 24 | + interactionContextProvider = weakInteractionContextProvider(context as? FragmentActivity), |
| 25 | + marker = Unit, |
| 26 | + ) |
| 27 | + |
| 28 | + /** Creates a factory that resolves the current activity after configuration changes. */ |
| 29 | + public constructor( |
| 30 | + context: Context, |
| 31 | + interactionContextProvider: () -> FragmentActivity?, |
| 32 | + ) : this( |
| 33 | + applicationContext = context.applicationContext, |
| 34 | + interactionContextProvider = interactionContextProvider, |
| 35 | + marker = Unit, |
| 36 | + ) |
| 37 | + |
15 | 38 | /** |
16 | 39 | * Creates an Android mobile wallet for [config]. |
17 | 40 | * |
18 | 41 | * The database is named from [MobileWalletConfig.walletId], and signing keys are created or loaded |
19 | 42 | * through the Android platform key provider. |
20 | 43 | */ |
21 | 44 | public actual suspend fun create(config: MobileWalletConfig): MobileWallet { |
22 | | - val driverFactory = DriverFactory(context) |
| 45 | + val driverFactory = DriverFactory(applicationContext) |
23 | 46 | return createEncryptedSqlDelightMobileWallet( |
24 | 47 | config = config, |
25 | | - managedDatabaseKeyProvider = AndroidDatabaseEncryptionKeyProvider(context), |
| 48 | + managedDatabaseKeyProvider = AndroidDatabaseEncryptionKeyProvider(applicationContext), |
26 | 49 | platformKeyProvider = AndroidPlatformKeyProvider( |
27 | | - context = context, |
| 50 | + context = applicationContext, |
| 51 | + interactionContextProvider = interactionContextProvider, |
28 | 52 | authorizationPrompt = config.keyUseAuthorizationPrompt, |
29 | 53 | ), |
30 | 54 | openEncryptedDriver = driverFactory::createEncryptedDriver, |
31 | 55 | deleteDatabase = driverFactory::deleteDatabase, |
32 | 56 | ) |
33 | 57 | } |
| 58 | + |
| 59 | + private companion object { |
| 60 | + fun weakInteractionContextProvider(activity: FragmentActivity?): () -> FragmentActivity? { |
| 61 | + val reference = activity?.let(::WeakReference) |
| 62 | + return { reference?.get() } |
| 63 | + } |
| 64 | + } |
34 | 65 | } |
0 commit comments