From ab63f87d7c52ef8decd2e6a5678dfd98387cbd42 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 26 Feb 2019 15:37:27 +0400 Subject: [PATCH] Added an extended class Application to get the Context from anywhere in the application, the DHT configuration (data storage folder) has been changed. --- .idea/misc.xml | 2 +- .../influence/ExtendedApplication.java | 22 +++++++++++++++++++ .../kademlia/DefaultConfiguration.java | 6 +++-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/io/github/chronosx88/influence/ExtendedApplication.java diff --git a/.idea/misc.xml b/.idea/misc.xml index af0bbdd..703e5d4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/java/io/github/chronosx88/influence/ExtendedApplication.java b/app/src/main/java/io/github/chronosx88/influence/ExtendedApplication.java new file mode 100644 index 0000000..93f32df --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/ExtendedApplication.java @@ -0,0 +1,22 @@ +package io.github.chronosx88.influence; + +import android.app.Application; +import android.content.Context; + +/** + * Extended Application class which designed for getting Context from anywhere in the application. + */ + +public class ExtendedApplication extends Application { + private static Application instance; + + @Override + public void onCreate() { + super.onCreate(); + instance = this; + } + + public static Context getContext() { + return instance.getApplicationContext(); + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/kademlia/DefaultConfiguration.java b/app/src/main/java/io/github/chronosx88/influence/kademlia/DefaultConfiguration.java index 8824c80..737ff89 100644 --- a/app/src/main/java/io/github/chronosx88/influence/kademlia/DefaultConfiguration.java +++ b/app/src/main/java/io/github/chronosx88/influence/kademlia/DefaultConfiguration.java @@ -2,6 +2,8 @@ package io.github.chronosx88.influence.kademlia; import java.io.File; +import io.github.chronosx88.influence.ExtendedApplication; + /** * A set of Kademlia configuration parameters. Default values are * supplied and can be changed by the application as necessary. @@ -17,7 +19,7 @@ public class DefaultConfiguration implements KadConfiguration private final static int K = 5; private final static int RCSIZE = 3; private final static int STALE = 1; - private final static String LOCAL_FOLDER = "com/github/joshuakissoon/kademlia"; + private final static String LOCAL_FOLDER = "DHT_Data"; private final static boolean IS_TESTING = true; @@ -75,7 +77,7 @@ public class DefaultConfiguration implements KadConfiguration public String getNodeDataFolder(String ownerId) { /* Setup the main storage folder if it doesn't exist */ - String path = System.getProperty("user.home") + File.separator + DefaultConfiguration.LOCAL_FOLDER; + String path = ExtendedApplication.getContext().getFilesDir() + File.separator + DefaultConfiguration.LOCAL_FOLDER; File folder = new File(path); if (!folder.isDirectory()) {