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

Fix: Android build #20

Merged
merged 11 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated by expo-module-scripts
module.exports = require('expo-module-scripts/eslintrc.base.js');
7 changes: 4 additions & 3 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
nodejs 18.16.0
ruby 3.2.2
cocoapods 1.12.1
java openjdk-18.0.1.1
golang 1.19.7
cocoapods 1.15.2
java openjdk-17.0.2
golang 1.22.4
yarn 1.22.19
buf 1.39.0
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
SHELL := /bin/bash

# Get the temporary directory of the system
TMPDIR := $(shell dirname $(shell mktemp -u))

# Define the directory that contains the current Makefile
make_dir := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
cache_dir := $(make_dir)/.cache

# Argument Defaults
IOS_OUTPUT_FRAMEWORK_DIR ?= $(make_dir)/ios/Frameworks
ANDROID_OUTPUT_LIBS_DIR ?= $(make_dir)/android/libs
PROTOCOLTYPES_COMMIT_HASH ?= c72d5759847b4dedb5411c19485e1a37
PROTOCOLTYPES_COMMIT_HASH ?= 62415a228a054c23a397b1c6689392d1
GO_BIND_BIN_DIR ?= $(cache_dir)/bind

# IOS definitions
Expand Down Expand Up @@ -71,16 +74,16 @@ _api.generate.protocol: src/api/protocoltypes.pb.js \
src/api/protocoltypes.pb.d.ts \
src/weshnet.types.gen.ts
_api.clean.protocol:
rm -f src/api/protocoltypes.pb.js src/api/protocoltypes.pb.d.ts src/weshnet.types.gen.ts
rm -f src/vendor src/api/protocoltypes.pb.js src/api/protocoltypes.pb.d.ts src/weshnet.types.gen.ts

api/protocoltypes.proto: buf.yaml
api/vendor/protocoltypes.proto:
mkdir -p $(dir $@)
buf export buf.build/berty/weshnet:$(PROTOCOLTYPES_COMMIT_HASH) --output $(dir $@)
src/api/protocoltypes.pb.js: api/protocoltypes.proto
buf export buf.build/berty-technologies/weshnet:$(PROTOCOLTYPES_COMMIT_HASH) --output $(dir $@)
src/api/protocoltypes.pb.js: api/vendor/protocoltypes.proto
$(pbjs) -t json-module -w es6 -o $@ $<
src/api/protocoltypes.pb.d.ts: api/protocoltypes.proto
src/api/protocoltypes.pb.d.ts: api/vendor/protocoltypes.proto
$(pbjs) -t static-module $< | $(pbts) -o $@ -
src/weshnet.types.gen.ts: api/protocoltypes.proto gen-clients.js
src/weshnet.types.gen.ts: api/vendor/protocoltypes.proto gen-clients.js
node gen-clients.js > $@

# - API - rpcmanager
Expand Down
70 changes: 45 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,73 @@ Wesh Network Expo Module
<a href="https://twitter.com/weshnet"><img alt="twitter" src="https://img.shields.io/twitter/follow/berty?label=%40weshnet&style=flat&logo=twitter" /></a>
</p>

<h3 align="center">The Wesh network toolkit lets your application use the Wesh protocol to support privacy-based, off-grid, peer-to-peer communication.
<h3 align="center">The Wesh network toolkit lets your application use the Wesh protocol to support privacy-based, off-grid, peer-to-peer communication.
<br/><br/>For details, see the Wesh website at <a href="https://wesh.network">https://wesh.network</a>. The website includes blog tutorials which introduce you to Wesh and walk you through some example applications and background of the Wesh protocol.</h3>

---

## Requirements

- [Go](https://golang.org/doc/install) >= 1.19.7
- [Go](https://golang.org/doc/install) = 1.22.4

Follow the [React Native requirements](https://reactnative.dev/docs/set-up-your-environment) and [Expo requirements](https://docs.expo.dev/get-started/set-up-your-environment) for your platform.

## Instalation

Install the package in your project:

```sh
# Expose ios and android native modules
# Ignore this step if you already have the 'ios' and 'android' folders in your project.
$ npx expo prebuild

# Install the package
$ npx expo install @berty/weshnet-expo

# Install the pods (it will run gomobile bind)
$ cd ios && pod install

# Run the app
$ npx expo run:ios
# create new expo project
npx create-expo-app my-app --template expo-template-blank-typescript
cd my-app
npx expo install @berty/weshnet-expo
```

## Usage

Add the following to your `App.js`:
Add the following to your `App.tsx`:

```tsx
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import * as WeshnetExpo from '@berty/weshnet-expo';
import React, { useState, useEffect } from "react";
import { StyleSheet, Text, View } from "react-native";

import * as WeshnetExpo from "@berty/weshnet-expo";

export default function App() {
return (
<View style={styles.container}>
<Text>{WeshnetExpo.hello('berty')}</Text>
<Text>Open up App.js to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
const [peerID, setPeerID] = useState<string>();
useEffect(() => {
WeshnetExpo.init().then((client) => {
client.serviceGetConfiguration({}).then((res) => {
setPeerID(res.peerId);
console.log(res);
});
});
}, []);

const loadingView = <Text> Loading Weshnet... </Text>;
const weshView = <Text>hello my peerid is: {peerID}</Text>;
return (
<View style={styles.container}>{!peerID ? loadingView : weshView}</View>
);

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
}
```

## Example App

```sh
cd example
make build.ios # or make build.android
```

## Contributing

[![Contribute to Berty](https://assets.berty.tech/files/contribute-contribute_v2--Contribute-berty-ultra-light.gif)](https://github.com/berty/community)
Expand Down
17 changes: 10 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ afterEvaluate {
android {
compileSdkVersion safeExtGet("compileSdkVersion", 33)

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
if (agpVersion.tokenize('.')[0].toInteger() < 8) {
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.majorVersion
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.majorVersion
}
}

namespace "expo.modules.weshnetexpo"
defaultConfig {
Expand Down
107 changes: 107 additions & 0 deletions android/src/main/java/expo/modules/weshnetexpo/ConnectivityDriver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package expo.modules.weshnetexpo

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.telephony.TelephonyManager
import android.util.Log
import androidx.core.app.ActivityCompat
import network.weshnet.core.ConnectivityInfo
import network.weshnet.core.Core
import network.weshnet.core.IConnectivityDriver
import network.weshnet.core.IConnectivityHandler

class ConnectivityDriver(context: Context) : BroadcastReceiver(), IConnectivityDriver {
private val mHandlers: ArrayList<IConnectivityHandler?>
private var mCurrentState: ConnectivityInfo? = null

init {
Log.d(TAG, "Init")
mHandlers = ArrayList<IConnectivityHandler?>()
updateState(context)
}

private fun getState(context: Context): ConnectivityInfo {
val state = ConnectivityInfo()
val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = manager.activeNetwork
state.bluetooth = bluetoothState
if (network == null) {
state.state = Core.ConnectivityStateOff
return state
}
val capabilities = manager.getNetworkCapabilities(network)
state.state = Core.ConnectivityStateOn
state.metering = if (capabilities!!.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) Core.ConnectivityStateOff else Core.ConnectivityStateOn
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
state.netType = Core.ConnectivityNetEthernet
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
state.netType = Core.ConnectivityNetWifi
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
state.netType = Core.ConnectivityNetCellular
state.cellularType = getCellularType(context)
}
return state
}

private fun updateState(context: Context) {
mCurrentState = getState(context)
for (handler in mHandlers) {
handler!!.handleConnectivityUpdate(mCurrentState)
}
}

override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "Network state changed")
updateState(context)
}

override fun getCurrentState(): ConnectivityInfo {
return mCurrentState!!
}

override fun registerHandler(handler: IConnectivityHandler) {
mHandlers.add(handler)
}

companion object {
private const val TAG = "ConnectivityDriver"
private fun getCellularType(context: Context): Long {
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "Getting cellular type: permission denied")
return Core.ConnectivityCellularUnknown
}
val netType: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tm.dataNetworkType
} else {
tm.networkType
}
return when (netType) {
TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_GSM -> Core.ConnectivityCellular2G
TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_TD_SCDMA -> Core.ConnectivityCellular3G
TelephonyManager.NETWORK_TYPE_LTE, TelephonyManager.NETWORK_TYPE_IWLAN, 19 -> Core.ConnectivityCellular4G
TelephonyManager.NETWORK_TYPE_NR -> Core.ConnectivityCellular5G
else -> Core.ConnectivityCellularUnknown
}
}

private val bluetoothState: Long
private get() {
val bluetooth = BluetoothAdapter.getDefaultAdapter()
return if (bluetooth == null || !bluetooth.isEnabled) {
Core.ConnectivityStateOff
} else when (bluetooth.state) {
BluetoothAdapter.STATE_ON -> Core.ConnectivityStateOn
BluetoothAdapter.STATE_OFF -> Core.ConnectivityStateOff
else -> Core.ConnectivityStateUnknown
}
}
}
}
23 changes: 23 additions & 0 deletions android/src/main/java/expo/modules/weshnetexpo/MDNSLockerDriver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package expo.modules.weshnetexpo

import android.content.Context
import android.net.wifi.WifiManager
import android.net.wifi.WifiManager.MulticastLock
import network.weshnet.core.NativeMDNSLockerDriver

class MDNSLockerDriver(private val context: Context) : NativeMDNSLockerDriver {
private lateinit var multicastLock: MulticastLock
override fun lock() {
val wifi = context.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
multicastLock = wifi.createMulticastLock("WeshnetMDNSLock")
multicastLock.setReferenceCounted(true)
multicastLock.acquire()
}

override fun unlock() {
if (multicastLock != null) {
multicastLock!!.release()
multicastLock
}
}
}
63 changes: 63 additions & 0 deletions android/src/main/java/expo/modules/weshnetexpo/NetDriver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package expo.modules.weshnetexpo

import android.util.Log
import network.weshnet.core.Core
import network.weshnet.core.NativeNetDriver
import network.weshnet.core.NetAddrs
import network.weshnet.core.NetInterface
import network.weshnet.core.NetInterfaces
import java.net.NetworkInterface
import java.util.Collections

class NetDriver : NativeNetDriver {
@Throws(Exception::class)
override fun interfaceAddrs(): NetAddrs {
val netaddrs = NetAddrs()
for (nif in Collections.list<NetworkInterface>(NetworkInterface.getNetworkInterfaces())) {
try {
for (ia in nif.interfaceAddresses) {
val parts = ia.toString().split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (parts.size > 1) {
netaddrs.appendAddr(parts[1])
}
}
} catch (ignored: Exception) {
}
}

return netaddrs
}

@Throws(Exception::class)
override fun interfaces(): NetInterfaces {
val ifaces = NetInterfaces()
for (nif in Collections.list<NetworkInterface>(NetworkInterface.getNetworkInterfaces())) {
val iface = NetInterface()
try {
iface.copyHardwareAddr(nif.hardwareAddress)
} catch (ignored: Exception) {
}
iface.index = nif.index.toLong()
iface.mtu = nif.mtu.toLong()
iface.name = nif.name
if (nif.isLoopback) {
iface.addFlag(Core.NetFlagLoopback)
}
if (nif.isPointToPoint) {
iface.addFlag(Core.NetFlagPointToPoint)
}
if (nif.isUp) {
iface.addFlag(Core.NetFlagUp)
}
if (nif.isVirtual) {
// iface.addFlag(Net);
}
if (nif.supportsMulticast()) {
iface.addFlag(Core.NetFlagMulticast)
}
ifaces.append(iface)
}

return ifaces
}
}
Loading