From 47b23611ae96f2308e2c0a98a6a97a368c244a98 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 7 Mar 2019 17:45:11 +0400 Subject: [PATCH] Migrate to AndroidX and storing of PeerID --- .idea/gradle.xml | 9 +- .idea/misc.xml | 34 ++++++-- .idea/render.experimental.xml | 6 -- .idea/vcs.xml | 2 +- app/build.gradle | 14 ++-- app/src/main/AndroidManifest.xml | 3 +- .../chronosx88/influence/MainActivity.java | 82 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 2 +- gradle.properties | 3 + 9 files changed, 131 insertions(+), 24 deletions(-) delete mode 100644 .idea/render.experimental.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 2996d53..7ac24c7 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,11 +3,14 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index af0bbdd..b0c7b20 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,11 +1,35 @@ - - - - + + + - + diff --git a/.idea/render.experimental.xml b/.idea/render.experimental.xml deleted file mode 100644 index 8ec256a..0000000 --- a/.idea/render.experimental.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index ffea97d..36a5460 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { targetSdkVersion 28 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { @@ -16,17 +16,19 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + packagingOptions { + exclude 'META-INF/INDEX.LIST' + exclude 'META-INF/io.netty.versions.properties' + } } dependencies { def room_version = "2.1.0-alpha04" implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' + implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" implementation 'net.tomp2p:tomp2p-all:5.0-Beta8' + implementation 'net.tomp2p:tomp2p-android:5.0-Beta8' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 639c50c..6048756 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/MainActivity.java index dde7688..2ede561 100644 --- a/app/src/main/java/io/github/chronosx88/influence/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/MainActivity.java @@ -1,13 +1,93 @@ package io.github.chronosx88.influence; -import android.support.v7.app.AppCompatActivity; +import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; + +import net.tomp2p.connection.RSASignatureFactory; +import net.tomp2p.dht.PeerBuilderDHT; +import net.tomp2p.dht.PeerDHT; +import net.tomp2p.dht.Storage; +import net.tomp2p.dht.StorageLayer; +import net.tomp2p.dht.StorageMemory; +import net.tomp2p.futures.FutureBootstrap; +import net.tomp2p.futures.FutureDiscover; +import net.tomp2p.p2p.PeerBuilder; +import net.tomp2p.peers.Number160; +import net.tomp2p.peers.Number320; +import net.tomp2p.peers.Number480; +import net.tomp2p.peers.Number640; +import net.tomp2p.storage.Data; +import net.tomp2p.storage.StorageDisk; + +import org.mapdb.DBMaker; + +import java.io.File; +import java.io.IOException; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.security.PublicKey; +import java.util.Collection; +import java.util.NavigableMap; +import java.util.UUID; + +import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { + private PeerDHT peerDHT; + private Number160 peerID; + private SharedPreferences preferences; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + preferences = getPreferences(MODE_PRIVATE); + + if(checkFirstRun()) { + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("peerID", UUID.randomUUID().toString()); + editor.apply(); + } else { + peerID = Number160.createHash(preferences.getString("peerID", "0")); + } + + try { + peerDHT = new PeerBuilderDHT( + new PeerBuilder(peerID) + .ports(7243) + .behindFirewall(true) + .start() + ) + .storage(new StorageDisk(peerID, getFilesDir(), new RSASignatureFactory())) + .start(); + InetAddress address = Inet4Address.getByName("192.168.0.82"); + FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress( address ).ports( 7243 ).start(); + futureDiscover.awaitUninterruptibly(); + FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress( address ).ports( 7243 ).start(); + futureBootstrap.awaitUninterruptibly(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public boolean checkFirstRun() { + if (preferences.getBoolean("firstRun", true)) { + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("firstRun", false); + editor.apply(); + return true; + } + return false; + } + + @Override + protected void onStop() { + super.onStop(); + Log.wtf("MainActivity", "onStop"); + peerDHT.shutdown(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0763a12..a163700 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ -