From fd7c4dc5dcf7c5b5f672166e832811b999b3c7d8 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Fortune Date: Tue, 5 Dec 2023 16:58:23 +0100 Subject: [PATCH] refactor: improve code readability --- .../reload/remote/data/ReaderRepository.kt | 7 +++--- .../reload/remote/ui/AbstractCardActivity.kt | 25 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/data/ReaderRepository.kt b/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/data/ReaderRepository.kt index a4f7ef5..3b329d1 100644 --- a/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/data/ReaderRepository.kt +++ b/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/data/ReaderRepository.kt @@ -13,6 +13,7 @@ package org.calypsonet.keyple.demo.reload.remote.data import kotlin.jvm.Throws import org.eclipse.keyple.core.common.KeyplePluginExtensionFactory +import org.eclipse.keyple.core.service.Plugin import org.eclipse.keyple.core.service.SmartCardServiceProvider import org.eclipse.keypop.reader.CardReader import org.eclipse.keypop.reader.ObservableCardReader @@ -26,11 +27,11 @@ import timber.log.Timber object ReaderRepository { /** Register any keyple plugin */ - fun registerPlugin(factory: KeyplePluginExtensionFactory) { - try { + fun registerPlugin(factory: KeyplePluginExtensionFactory): Plugin? { + return try { SmartCardServiceProvider.getService().registerPlugin(factory) } catch (e: Exception) { - Timber.e(e) + null } } diff --git a/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/ui/AbstractCardActivity.kt b/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/ui/AbstractCardActivity.kt index 44300da..6fdd154 100644 --- a/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/ui/AbstractCardActivity.kt +++ b/client/android/app/src/main/kotlin/org/calypsonet/keyple/demo/reload/remote/ui/AbstractCardActivity.kt @@ -21,6 +21,7 @@ import org.calypsonet.keyple.demo.reload.remote.data.model.CardReaderResponse import org.calypsonet.keyple.demo.reload.remote.data.model.DeviceEnum import org.calypsonet.keyple.demo.reload.remote.data.model.Status import org.eclipse.keyple.core.service.KeyplePluginException +import org.eclipse.keyple.core.service.Plugin import org.eclipse.keyple.distributed.LocalServiceClient import org.eclipse.keyple.plugin.android.nfc.AndroidNfcPlugin import org.eclipse.keyple.plugin.android.nfc.AndroidNfcPluginFactoryProvider @@ -88,18 +89,24 @@ abstract class AbstractCardActivity : /** Android Nfc Reader is strongly dependent and Android Activity component. */ @Throws(KeyplePluginException::class) fun initAndActivateAndroidKeypleNfcReader() { - readerRepository.registerPlugin( - AndroidNfcPluginFactoryProvider(this@AbstractCardActivity).getFactory()) + val plugin: Plugin? = + readerRepository.registerPlugin( + AndroidNfcPluginFactoryProvider(this@AbstractCardActivity).getFactory()) - val androidNfcReader = readerRepository.getObservableReader(selectedDeviceReaderName) - androidNfcReader.setReaderObservationExceptionHandler(this@AbstractCardActivity) - androidNfcReader.addObserver(this@AbstractCardActivity) - androidNfcReader.setReaderObservationExceptionHandler(this@AbstractCardActivity) + if (plugin == null) { + return + } - (readerRepository.getReader(selectedDeviceReaderName) as ConfigurableCardReader) - .activateProtocol(AndroidNfcSupportedProtocols.ISO_14443_4.name, "ISO_14443_4") + val observableCardReader: ObservableCardReader = + readerRepository.getObservableReader(selectedDeviceReaderName) + observableCardReader.setReaderObservationExceptionHandler(this@AbstractCardActivity) + observableCardReader.addObserver(this@AbstractCardActivity) + observableCardReader.setReaderObservationExceptionHandler(this@AbstractCardActivity) + + (observableCardReader as ConfigurableCardReader).activateProtocol( + AndroidNfcSupportedProtocols.ISO_14443_4.name, "ISO_14443_4") - androidNfcReader.startCardDetection(ObservableCardReader.DetectionMode.REPEATING) + observableCardReader.startCardDetection(ObservableCardReader.DetectionMode.REPEATING) } @Throws(KeyplePluginException::class)