Craig Raw
5 years ago
12 changed files with 194 additions and 53 deletions
@ -0,0 +1,99 @@ |
|||
package com.sparrowwallet.sparrow.io; |
|||
|
|||
import com.google.gson.*; |
|||
import com.sparrowwallet.drongo.Utils; |
|||
|
|||
import java.io.*; |
|||
import java.lang.reflect.Type; |
|||
|
|||
public class Config { |
|||
public static final String CONFIG_FILENAME = ".config"; |
|||
|
|||
private Integer keyDerivationPeriod; |
|||
private File hwi; |
|||
|
|||
private static Config INSTANCE; |
|||
|
|||
private static Gson getGson() { |
|||
GsonBuilder gsonBuilder = new GsonBuilder(); |
|||
gsonBuilder.registerTypeAdapter(File.class, new FileSerializer()); |
|||
gsonBuilder.registerTypeAdapter(File.class, new FileDeserializer()); |
|||
return gsonBuilder.setPrettyPrinting().disableHtmlEscaping().create(); |
|||
} |
|||
|
|||
private static File getConfigFile() { |
|||
return new File(Storage.getSparrowDir(), CONFIG_FILENAME); |
|||
} |
|||
|
|||
private static Config load() { |
|||
File configFile = getConfigFile(); |
|||
if(configFile.exists()) { |
|||
try { |
|||
Reader reader = new FileReader(configFile); |
|||
Config config = getGson().fromJson(reader, Config.class); |
|||
reader.close(); |
|||
|
|||
if(config != null) { |
|||
return config; |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
//Ignore and assume no config
|
|||
} |
|||
} |
|||
|
|||
return new Config(); |
|||
} |
|||
|
|||
public static synchronized Config get() { |
|||
if(INSTANCE == null) { |
|||
INSTANCE = load(); |
|||
} |
|||
|
|||
return INSTANCE; |
|||
} |
|||
|
|||
public Integer getKeyDerivationPeriod() { |
|||
return keyDerivationPeriod; |
|||
} |
|||
|
|||
public void setKeyDerivationPeriod(Integer keyDerivationPeriod) { |
|||
this.keyDerivationPeriod = keyDerivationPeriod; |
|||
flush(); |
|||
} |
|||
|
|||
public File getHwi() { |
|||
return hwi; |
|||
} |
|||
|
|||
public void setHwi(File hwi) { |
|||
this.hwi = hwi; |
|||
flush(); |
|||
} |
|||
|
|||
private void flush() { |
|||
Gson gson = getGson(); |
|||
try { |
|||
File configFile = getConfigFile(); |
|||
Writer writer = new FileWriter(configFile); |
|||
gson.toJson(this, writer); |
|||
writer.close(); |
|||
} catch (IOException e) { |
|||
//Ignore
|
|||
} |
|||
} |
|||
|
|||
private static class FileSerializer implements JsonSerializer<File> { |
|||
@Override |
|||
public JsonElement serialize(File src, Type typeOfSrc, JsonSerializationContext context) { |
|||
return new JsonPrimitive(src.getAbsolutePath()); |
|||
} |
|||
} |
|||
|
|||
private static class FileDeserializer implements JsonDeserializer<File> { |
|||
@Override |
|||
public File deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
|||
return new File(json.getAsJsonPrimitive().getAsString()); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue