From 64a527df78c7dcd82a876584935ddbf18d105864 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 15 Mar 2019 22:09:57 +0400 Subject: [PATCH] Storing peerID and peerDHT in static class, showing peerID in settings --- .../chronosx88/influence/helpers/AppHelper.java | 12 ++++++++++++ .../chronosx88/influence/models/MainModel.java | 5 ++--- .../views/fragments/SettingsFragment.java | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java index ec22ae9..f198d0d 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java @@ -3,6 +3,8 @@ package io.github.chronosx88.influence.helpers; import android.app.Application; import android.content.Context; +import net.tomp2p.dht.PeerDHT; + import io.github.chronosx88.influence.observable.MainObservable; /** @@ -12,6 +14,8 @@ import io.github.chronosx88.influence.observable.MainObservable; public class AppHelper extends Application { private static Application instance; private static MainObservable observable; + private static String peerID; + private static PeerDHT peerDHT; @Override public void onCreate() { @@ -20,9 +24,17 @@ public class AppHelper extends Application { observable = new MainObservable(); } + public static void storePeerID(String peerID1) { peerID = peerID1; } + + public static void storePeerDHT(PeerDHT peerDHT1) { peerDHT = peerDHT1; } + public static Context getContext() { return instance.getApplicationContext(); } public static MainObservable getObservable() { return observable; } + + public static String getPeerID() { return peerID; } + + public static PeerDHT getPeerDHT() { return peerDHT; } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java index 712b554..9ee5296 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java @@ -8,7 +8,6 @@ import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerDHT; import net.tomp2p.futures.FutureBootstrap; import net.tomp2p.futures.FutureDiscover; -import net.tomp2p.nat.FutureNAT; import net.tomp2p.nat.FutureRelayNAT; import net.tomp2p.nat.PeerBuilderNAT; import net.tomp2p.nat.PeerNAT; @@ -24,8 +23,6 @@ import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Collection; import java.util.UUID; import io.github.chronosx88.influence.contracts.MainModelContract; @@ -131,6 +128,8 @@ public class MainModel implements MainModelContract { } catch (JSONException ex) { ex.printStackTrace(); } + AppHelper.storePeerID(preferences.getString("peerID", null)); + AppHelper.storePeerDHT(peerDHT); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/fragments/SettingsFragment.java b/app/src/main/java/io/github/chronosx88/influence/views/fragments/SettingsFragment.java index 8a74232..bda3782 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/fragments/SettingsFragment.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/fragments/SettingsFragment.java @@ -1,13 +1,28 @@ package io.github.chronosx88.influence.views.fragments; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.os.Bundle; +import android.widget.Toast; import androidx.preference.PreferenceFragmentCompat; import io.github.chronosx88.influence.R; +import io.github.chronosx88.influence.helpers.AppHelper; public class SettingsFragment extends PreferenceFragmentCompat { + @Override public void onCreatePreferences(Bundle bundle, String s) { - setPreferencesFromResource(R.xml.main_settings, s); + // Load the Preferences from the XML file + addPreferencesFromResource(R.xml.main_settings); + getPreferenceScreen().getPreference(0).setSummary(AppHelper.getPeerID()); + getPreferenceScreen().getPreference(0).setOnPreferenceClickListener((preference -> { + ClipboardManager clipboard = (ClipboardManager) AppHelper.getContext().getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("", AppHelper.getPeerID()); + clipboard.setPrimaryClip(clip); + Toast.makeText(AppHelper.getContext(), "Скопировано в буфер обмена!", Toast.LENGTH_SHORT).show(); + return false; + })); } }