Skip to content

Commit

Permalink
Inji tuvali release (mosip#1557)
Browse files Browse the repository at this point in the history
* Revert "[INJIMOB-891] add tuvali native"

This reverts commit d4a4e89bd64f2c753e22576aeff4840fcb1eb0ae.

* [INJIMOB-891]: add a wrapper for tuvali to use native artifacts

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-891]: add a wrapper for tuvali in ios to consume ios native artifact

Signed-off-by: Alka Prasad <[email protected]>

* [INJIMOB-891]: add tuvali native dependency

Signed-off-by: Alka Prasad <[email protected]>

---------

Signed-off-by: Alka Prasad <[email protected]>
Co-authored-by: Alka Prasad <[email protected]>
  • Loading branch information
abhip2565 and Alka1703 authored Jul 10, 2024
1 parent 3262795 commit 0b11e0a
Show file tree
Hide file tree
Showing 30 changed files with 746 additions and 92 deletions.
12 changes: 12 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ dependencies {
implementation 'com.facebook.soloader:soloader:0.10.1+'
implementation("io.mosip:pixelpass:0.2.0-SNAPSHOT")
implementation("io.mosip:secure-keystore:0.2.0-SNAPSHOT")
implementation("io.mosip:tuvali:0.5.0-SNAPSHOT")

def isGifEnabled = (findProperty('expo.gif.enabled') ?: "") == "true";
def isWebpEnabled = (findProperty('expo.webp.enabled') ?: "") == "true";
Expand Down Expand Up @@ -304,6 +305,17 @@ dependencies {
implementation 'com.jakewharton.timber:timber:4.7.1'
}

task CaptureLibraryVersion {
def libDef = project.configurations.getByName('implementation').allDependencies.matching {
it.group.equals("io.mosip") && it.name.equals("tuvali")
}
if (libDef.size() > 0) {
android.buildTypes.each {
it.buildConfigField 'String', 'TUVALI_LIB_VERSION', "\"${libDef[0].version}\""
}
}
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

// https://github.com/oblador/react-native-vector-icons?tab=readme-ov-file#android-setup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.mosip.residentapp;

import com.facebook.react.bridge.WritableMap;

import kotlin.Unit;

public interface IRNEventEmitter {
Unit emitEvent(WritableMap eventMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.util.Collections;
import java.util.List;


import io.mosip.tuvali.verifier.Verifier;
import io.mosip.tuvali.wallet.Wallet;

public class InjiPackage implements ReactPackage {
@NonNull
@Override
Expand All @@ -20,6 +22,9 @@ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext r
modules.add(new InjiVciClientModule(reactApplicationContext));
modules.add(new RNPixelpassModule(reactApplicationContext));
modules.add(new RNSecureKeystoreModule(reactApplicationContext));
modules.add(new RNVersionModule());
modules.add(new RNWalletModule(new RNEventEmitter(reactApplicationContext), new Wallet(reactApplicationContext), reactApplicationContext));
modules.add(new RNVerifierModule(new RNEventEmitter(reactApplicationContext), new Verifier(reactApplicationContext), reactApplicationContext));
return modules;
}

Expand Down
24 changes: 24 additions & 0 deletions android/app/src/main/java/io/mosip/residentapp/RNEventEmitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.mosip.residentapp;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class RNEventEmitter implements IRNEventEmitter {

private static final String EVENT_NAME = "DATA_EVENT";
private final ReactApplicationContext reactContext;

public RNEventEmitter(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

@Override
public Unit emitEvent(WritableMap eventMap) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(EVENT_NAME, eventMap);
return null;
}
}
74 changes: 74 additions & 0 deletions android/app/src/main/java/io/mosip/residentapp/RNEventMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.mosip.residentapp;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import io.mosip.tuvali.common.events.*;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.HashMap;

public class RNEventMapper {

public static WritableMap toMap(Event event) {
WritableMap writableMap = Arguments.createMap();
writableMap.putString("type", getEventType(event));
populateProperties(event, writableMap);
return writableMap;
}

private static String getEventType(Event event) {
if (event instanceof DataReceivedEvent) {
return "onDataReceived";
} else if (event instanceof ErrorEvent) {
return "onError";
} else if (event instanceof VerificationStatusEvent) {
return "onVerificationStatusReceived";
} else if (event instanceof ConnectedEvent) {
return "onConnected";
} else if (event instanceof DataSentEvent) {
return "onDataSent";
} else if (event instanceof DisconnectedEvent) {
return "onDisconnected";
} else if (event instanceof SecureChannelEstablishedEvent) {
return "onSecureChannelEstablished";
} else {
System.out.println("Invalid event type");
return "";
}
}

private static void populateProperties(Event event, WritableMap writableMap) {
for (Field field : event.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
populateProperty(field, event, writableMap);
field.setAccessible(false);
} catch (Exception e) {
System.out.println("Unable to populate RN event " + field.getName());
}
}
}

private static void populateProperty(Field field, Event event, WritableMap writableMap) throws IllegalAccessException {
Object propertyValue = field.get(event);
if (propertyValue instanceof Enum) {
propertyValue = readEnumValue((Enum<?>) propertyValue);
}
if (propertyValue instanceof Integer) {
writableMap.putInt(field.getName(), (Integer) propertyValue);
} else {
writableMap.putString(field.getName(), propertyValue.toString());
}
}

private static Object readEnumValue(Enum<?> enumValue) {
try {
Field valueField = enumValue.getClass().getDeclaredField("value");
valueField.setAccessible(true);
return valueField.get(enumValue);
} catch (Exception e) {
return enumValue.ordinal();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.mosip.residentapp;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import io.mosip.tuvali.verifier.Verifier;

public class RNVerifierModule extends ReactContextBaseJavaModule {

private static final String NAME = "VerifierModule";
private final RNEventEmitter eventEmitter;
private final Verifier verifier;


public RNVerifierModule(RNEventEmitter eventEmitter, Verifier verifier, ReactApplicationContext reactContext) {
super(reactContext);
this.eventEmitter = eventEmitter;
this.verifier = verifier;
verifier.subscribe(event -> eventEmitter.emitEvent(RNEventMapper.toMap(event)));
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String startAdvertisement(String advIdentifier) {
return verifier.startAdvertisement(advIdentifier);
}

@ReactMethod(isBlockingSynchronousMethod = true)
public void disconnect() {
verifier.disconnect();
}

@ReactMethod
public void sendVerificationStatus(int status) {
verifier.sendVerificationStatus(status);
}

@Override
public String getName() {
return NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package io.mosip.residentapp;

import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import io.mosip.residentapp.BuildConfig;



public class RNVersionModule extends ReactContextBaseJavaModule {

private static final String NAME = "VersionModule";

@ReactMethod(isBlockingSynchronousMethod = true)
public String getVersion() {
return BuildConfig.TUVALI_LIB_VERSION;
}

@Override
public String getName() {
return NAME;
}
}


82 changes: 82 additions & 0 deletions android/app/src/main/java/io/mosip/residentapp/RNWalletModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.mosip.residentapp;

import android.bluetooth.BluetoothAdapter;
import android.content.IntentFilter;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;

import io.mosip.residentapp.RNEventEmitter;
import io.mosip.tuvali.common.BluetoothStateChangeReceiver;
import io.mosip.tuvali.wallet.Wallet;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;

public class RNWalletModule extends ReactContextBaseJavaModule {

private static final String NAME = "WalletModule";
private final RNEventEmitter eventEmitter;
private final Wallet wallet;
private final IntentFilter bluetoothStateChangeIntentFilter;
private final BluetoothStateChangeReceiver bluetoothStateChangeReceiver;

public RNWalletModule(RNEventEmitter eventEmitter, Wallet wallet, ReactApplicationContext reactContext) {
super(reactContext);
this.eventEmitter = eventEmitter;
this.wallet = wallet;

this.bluetoothStateChangeIntentFilter = new IntentFilter();
bluetoothStateChangeIntentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

this.bluetoothStateChangeReceiver = new BluetoothStateChangeReceiver(handleDisconnect);
reactContext.registerReceiver(bluetoothStateChangeReceiver, bluetoothStateChangeIntentFilter);
wallet.subscribe(event -> eventEmitter.emitEvent(RNEventMapper.toMap(event)));
}

Function2<Integer, Integer, Unit> handleDisconnect = new Function2<Integer, Integer, Unit>() {
@Override
public Unit invoke(Integer status, Integer state) {
wallet.handleDisconnect(status, state);
return Unit.INSTANCE;
}
};

public Function1<WritableMap, Unit> emitEventHandler = new Function1<WritableMap, Unit>() {
@Override
public Unit invoke(WritableMap eventMap) {
eventEmitter.emitEvent(eventMap);
return Unit.INSTANCE;
}
};

@ReactMethod(isBlockingSynchronousMethod = true)
public void startConnection(String uri) {
wallet.startConnection(uri);
}

@ReactMethod
public void sendData(String payload) {
wallet.sendData(payload);
}

@ReactMethod(isBlockingSynchronousMethod = true)
public void disconnect() {
wallet.disconnect();
}

@Override
public String getName() {
return NAME;
}

@Override
protected void finalize() throws Throwable {
try {
getReactApplicationContext().unregisterReceiver(bluetoothStateChangeReceiver);
} finally {
super.finalize();
}
}
}
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ buildscript {
google()
jcenter()
mavenCentral()

}
dependencies {
classpath("com.android.tools.build:gradle:7.3.1")
Expand All @@ -28,5 +27,7 @@ buildscript {
def expoCameraMavenPath = new File(["node", "--print", "require.resolve('expo-camera/package.json')"].execute(null, rootDir).text.trim(), "../android/maven")
allprojects { repositories {
maven{url("https://oss.sonatype.org/content/repositories/snapshots/")}
maven { url(expoCameraMavenPath) } } }
maven { url(expoCameraMavenPath) }
mavenCentral()
} }
// @generated end expo-camera-import
Loading

0 comments on commit 0b11e0a

Please sign in to comment.