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

[#168485266] Removed 'any' and typed enums #7

Merged
merged 7 commits into from
Sep 24, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MainActivity : AppCompatActivity(), Callback {
//configurazione cieidsdk
CieIDSdk.start(this, this)
//passare il pin inserito dall'utente
CieIDSdk.pin = "81813213"
CieIDSdk.pin = "12345678"
//settare la url caricata dalla webview su /OpenApp
CieIDSdk.setUrl(url)
//abilitare i log
Expand Down
23 changes: 11 additions & 12 deletions cieidsdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,31 @@ class CieManager {
});
};

/**
* Return true if the nfc is enabled, on the device in Settings screen
* is possible enable or disable it.
*/
isNFCEnabled = () => {
if (Platform.OS === "ios") {
return Promise.reject("not implemented");
}
return new Promise((resolve, reject) => {
NativeCie.isNFCEnabled((err, result) => {
if (err) {
reject(err);
} else {
return new Promise(resolve => {
NativeCie.isNFCEnabled((result) => {
resolve(result);
}
});
});
};

/**
* Check if the hardware module nfc is installed (only for Android devices)
*/
hasNFCFeature = () => {
if (Platform.OS === "ios") {
return Promise.reject("not implemented");
}
return new Promise((resolve, reject) => {
NativeCie.hasNFCFeature((err, result) => {
if (err) {
reject(err);
} else {
return new Promise(resolve => {
NativeCie.hasNFCFeature((result) => {
resolve(result);
}
});
});
};
Expand Down
49 changes: 32 additions & 17 deletions cieidsdk/src/main/java/it/ipzs/cieidsdk/common/CieIDSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,39 @@ val CERTIFICATE_REVOKED: CharSequence = "SSLV3_ALERT_CERTIFICATE_REVOKED"

class Event {

enum class EventValue {
interface EventValue {
val nameEvent: String
}

enum class EventTag : EventValue {
//tag
ON_TAG_DISCOVERED_NOT_CIE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these enums must be mapped into index.d.ts

example

export type EventValue = 'ON_TAG_DISCOVERED_NOT_CIE' | 'ON_TAG_DISCOVERED' | 'ON_TAG_LOST' .. ...;

[...]

setEventListner(callback: (event: EventValue) => void): void;

(setEventListner should be changed in onEvent)

this is should be done for all events that onEvent function can send and for onError too we have, on React Native side, raise an Error with same description of native side. Indeed now a onError function is missing, you should add it.

interface Callback {

    fun onSuccess(url: String)
    fun onError(e: Throwable)
    fun onEvent(event: Event)
}

At the end check all these Promises I did Promise<never> (i.e startListeningNFC). I guess they can be improved

ON_TAG_DISCOVERED,
ON_TAG_LOST,
ON_TAG_LOST;

override val nameEvent: String = name
}
enum class EventCard: EventValue {
//card
ON_CARD_PIN_LOCKED,
ON_PIN_ERROR,
ON_PIN_ERROR;

override val nameEvent: String = name

}
enum class EventCertificate : EventValue {
//certificate
CERTIFICATE_EXPIRED,
CERTIFICATE_REVOKED,
CERTIFICATE_REVOKED;

override val nameEvent: String = name
}
enum class EventError : EventValue {
//error
AUTHENTICATION_ERROR,
GENERAL_ERROR,
ON_NO_INTERNET_CONNECTION
ON_NO_INTERNET_CONNECTION;

override val nameEvent: String = name
}

private var tentativi: Int = 0
Expand All @@ -67,7 +82,7 @@ class Event {
}

override fun toString(): String {
return eventValue.name
return eventValue.nameEvent
}
}

Expand All @@ -87,7 +102,7 @@ object CieIDSdk : NfcAdapter.ReaderCallback {
internal var ias: Ias? = null
var enableLog: Boolean = false
var pin: String = ""
private const val isoDepTimeout: Int = 2000
private const val isoDepTimeout: Int = 4000


@SuppressLint("CheckResult")
Expand All @@ -114,7 +129,7 @@ object CieIDSdk : NfcAdapter.ReaderCallback {
callback?.onSuccess(url)

} else {
callback?.onEvent(Event(Event.EventValue.AUTHENTICATION_ERROR))
callback?.onEvent(Event(Event.EventError.AUTHENTICATION_ERROR))
}

}
Expand All @@ -125,16 +140,16 @@ object CieIDSdk : NfcAdapter.ReaderCallback {
when (e) {
is SocketTimeoutException , is UnknownHostException -> {
CieIDSdkLogger.log("SocketTimeoutException or UnknownHostException")
callback?.onEvent(Event(Event.EventValue.ON_NO_INTERNET_CONNECTION))
callback?.onEvent(Event(Event.EventError.ON_NO_INTERNET_CONNECTION))

}
is SSLProtocolException -> {

CieIDSdkLogger.log("SSLProtocolException")
e.message?.let {
when {
it.contains(CERTIFICATE_EXPIRED) -> callback?.onEvent(Event(Event.EventValue.CERTIFICATE_EXPIRED))
it.contains(CERTIFICATE_REVOKED) -> callback?.onEvent(Event(Event.EventValue.CERTIFICATE_REVOKED))
it.contains(CERTIFICATE_EXPIRED) -> callback?.onEvent(Event(Event.EventCertificate.CERTIFICATE_EXPIRED))
it.contains(CERTIFICATE_REVOKED) -> callback?.onEvent(Event(Event.EventCertificate.CERTIFICATE_REVOKED))
else -> callback?.onError(e)
}
}
Expand All @@ -149,7 +164,7 @@ object CieIDSdk : NfcAdapter.ReaderCallback {

override fun onTagDiscovered(tag: Tag?) {
try {
callback?.onEvent(Event(Event.EventValue.ON_TAG_DISCOVERED))
callback?.onEvent(Event(Event.EventTag.ON_TAG_DISCOVERED))
val isoDep = IsoDep.get(tag)
isoDep.timeout = isoDepTimeout
isoDep.connect()
Expand All @@ -163,10 +178,10 @@ object CieIDSdk : NfcAdapter.ReaderCallback {
} catch (throwable: Throwable) {
CieIDSdkLogger.log(throwable.toString())
when (throwable) {
is PinNotValidException -> callback?.onEvent(Event(Event.EventValue.ON_PIN_ERROR, throwable.tentativi))
is BlockedPinException -> callback?.onEvent(Event(Event.EventValue.ON_CARD_PIN_LOCKED))
is NoCieException -> callback?.onEvent(Event(Event.EventValue.ON_TAG_DISCOVERED_NOT_CIE))
is TagLostException -> callback?.onEvent(Event(Event.EventValue.ON_TAG_LOST))
is PinNotValidException -> callback?.onEvent(Event(Event.EventCard.ON_PIN_ERROR, throwable.tentativi))
is BlockedPinException -> callback?.onEvent(Event(Event.EventCard.ON_CARD_PIN_LOCKED))
is NoCieException -> callback?.onEvent(Event(Event.EventTag.ON_TAG_DISCOVERED_NOT_CIE))
is TagLostException -> callback?.onEvent(Event(Event.EventTag.ON_TAG_LOST))
else -> callback?.onError(throwable)
}
}
Expand Down
28 changes: 20 additions & 8 deletions cieidsdk/src/main/java/it/ipzs/cieidsdk/native_bridge/CieModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,22 @@ class CieModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

@ReactMethod
fun start(callback: com.facebook.react.bridge.Callback){
CieIDSdk.start(getCurrentActivity()!!, this)
callback.invoke(null,null)
try {
CieIDSdk.start(getCurrentActivity()!!, this)
callback.invoke(null, null)
} catch (e: RuntimeException) {
callback.invoke(e.message,null)
}
}

@ReactMethod
fun isNFCEnabled(callback: com.facebook.react.bridge.Callback) {
callback.invoke(null,CieIDSdk.isNFCEnabled(getCurrentActivity()!!))
callback.invoke(CieIDSdk.isNFCEnabled(getCurrentActivity()!!))
}

@ReactMethod
fun hasNFCFeature(callback: com.facebook.react.bridge.Callback) {
callback.invoke(null,CieIDSdk.hasFeatureNFC(getCurrentActivity()!!))
callback.invoke(CieIDSdk.hasFeatureNFC(getCurrentActivity()!!))
}

@ReactMethod
Expand All @@ -70,14 +74,22 @@ class CieModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

@ReactMethod
fun startListeningNFC(callback: com.facebook.react.bridge.Callback) {
CieIDSdk.startNFCListening(getCurrentActivity()!!)
callback.invoke(null,null)
try {
CieIDSdk.startNFCListening(getCurrentActivity()!!)
callback.invoke(null,null)
} catch (e: RuntimeException) {
callback.invoke(e.message,null)
}
}

@ReactMethod
fun stopListeningNFC(callback: com.facebook.react.bridge.Callback) {
CieIDSdk.stopNFCListening(getCurrentActivity()!!)
callback.invoke(null,null)
try {
CieIDSdk.stopNFCListening(getCurrentActivity()!!)
callback.invoke(null, null)
} catch (e: RuntimeException) {
callback.invoke(e.message,null)
}
}

@ReactMethod
Expand Down