gnunet-android

GNUnet for Android
Log | Files | Refs | README

MainActivity.kt (4819B)


      1 package org.gnu.gnunet
      2 
      3 import android.content.Intent
      4 import android.content.res.AssetManager
      5 import android.os.Build
      6 import android.os.Bundle
      7 import android.util.Log
      8 import androidx.appcompat.app.AppCompatActivity
      9 import org.gnu.gnunet.core.GnunetCoreService
     10 import org.gnu.gnunet.databinding.ActivityMainBinding
     11 import java.io.File
     12 import java.io.FileOutputStream
     13 import java.io.IOException
     14 import java.io.InputStream
     15 
     16 class MainActivity : AppCompatActivity() {
     17 
     18     private lateinit var binding: ActivityMainBinding
     19 
     20     override fun onCreate(savedInstanceState: Bundle?) {
     21         super.onCreate(savedInstanceState)
     22 
     23         val dataPath: String = filesDir.getAbsolutePath()
     24         /*try {
     25             System.loadLibrary("gnunetutil") // Muss ganz früh geladen sein
     26             Log.d("DEBUG", "libgnunetutil.so geladen")
     27         } catch (e: UnsatisfiedLinkError) {
     28             Log.e("DEBUG", "Fehler beim Laden libgnunetutil", e)
     29         }
     30 
     31         try {
     32             System.loadLibrary("gnunet_plugin_peerstore_sqlite") // Muss ganz früh geladen sein
     33             Log.d("DEBUG", "libgnunet_plugin_peerstore_sqlite.so geladen")
     34         } catch (e: UnsatisfiedLinkError) {
     35             Log.e("DEBUG", "Fehler beim Laden libgnunet_plugin_peerstore_sqlite", e)
     36         }*/
     37 
     38         binding = ActivityMainBinding.inflate(layoutInflater)
     39         setContentView(binding.root)
     40 
     41         var isSuccess = copyFileFromAssetsToInternalStorage("plugins/libgnunet_plugin_peerstore_sqlite.so", "libgnunet_plugin_peerstore_sqlite.so")
     42         if (isSuccess) {
     43             Log.d("GNUNET", "onCreate: file libgnunet_plugin_peerstore_sqlite.so copied successfully")
     44         } else {
     45             Log.e("GNUNET", "onCreate: file libgnunet_plugin_peerstore_sqlite.so not copied :(")
     46         }
     47 
     48         isSuccess = copyFileFromAssetsToInternalStorage("private_key.ecc", "private_key.ecc")
     49         if (isSuccess) {
     50             Log.d("GNUNET", "onCreate: file private_key.ecc copied successfully")
     51         } else {
     52             Log.e("GNUNET", "onCreate: file private_key.ecc not copied :(")
     53         }
     54 
     55         Log.d("GNUNET", "is asset null? assets = $assets")
     56         // Example of a call to a native method
     57         val intent = Intent(this, GnunetCoreService::class.java)
     58         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     59             startForegroundService(intent)
     60         } else {
     61             startService(intent)
     62         }
     63         //binding.sampleText.text = stringFromJNI(assets, dataPath)
     64     }
     65 
     66     private fun copyFileFromAssetsToInternalStorage(fileName: String, outputFileName: String): Boolean {
     67         var inputStream: InputStream? = null
     68         var outputStream: FileOutputStream? = null
     69 
     70         return try {
     71             // Access the file from the assets
     72             inputStream = assets.open(fileName)
     73 
     74             // Create the destination file in internal storage
     75             val outputFile = File(filesDir, outputFileName)
     76             Log.d("GNUNET", outputFile.path)
     77             outputStream = FileOutputStream(outputFile)
     78 
     79             // Buffer to hold data during copy
     80             val buffer = ByteArray(1024)
     81             var length: Int
     82 
     83             // Copy the file contents from assets to the destination file
     84             while (inputStream.read(buffer).also { length = it } > 0) {
     85                 outputStream.write(buffer, 0, length)
     86             }
     87 
     88             outputFile.setReadable(true, false)
     89             outputFile.setExecutable(true, false)
     90             //Success
     91             true
     92         } catch (e: IOException) {
     93             e.printStackTrace()
     94 
     95             // Failure
     96             false
     97         } finally {
     98             inputStream?.close()
     99             outputStream?.close()
    100         }
    101     }
    102     /**
    103      * A native method that is implemented by the 'gnunet' native library,
    104      * which is packaged with this application.
    105      */
    106     external fun stringFromJNI(assets: AssetManager,
    107                                path: String): String
    108 
    109     companion object {
    110         // Used to load the 'gnunet' library on application startup.
    111         init {
    112             /*try {
    113 
    114 
    115             System.loadLibrary("sqliteX");
    116             System.loadLibrary("gnunetsq")
    117             System.loadLibrary("gnunetutil")
    118             System.loadLibrary("gnunetblock")
    119             System.loadLibrary("gnunetregexblock")
    120             System.loadLibrary("gnunetblockgroup")
    121             System.loadLibrary("gnunetgnsrecord")
    122             System.loadLibrary("gnunetjson")
    123             System.loadLibrary("gnunethello")
    124             System.loadLibrary("gnunet_plugin_peerstore_sqlite");
    125             System.loadLibrary("gnunet")
    126             } catch (e: UnsatisfiedLinkError) {
    127                 Log.e("NativeLoader", "Failed to load native library", e);
    128             }*/
    129             System.loadLibrary("gnunetti")
    130         }
    131     }
    132 }