WIP: work on improving node storage

This commit is contained in:
pokkst 2023-05-24 21:52:06 -05:00
parent 15704f9afa
commit beef898be4
No known key found for this signature in database
GPG key ID: 90C2ED85E67A50FF
12 changed files with 251 additions and 177 deletions

View file

@ -96,7 +96,6 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
}
public void init(File walletFile, String password) {
upgradeOldNodePrefs();
Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password);
thread = new MoneroHandlerThread("WalletService", this, wallet);
new TxService(thread);
@ -108,10 +107,6 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
thread.start();
}
private void upgradeOldNodePrefs() {
PrefService.getInstance().upgradeNodePrefs();
}
@Override
public void onRefresh() {
this.historyService.refreshHistory();

View file

@ -29,7 +29,6 @@ import net.mynero.wallet.R;
import net.mynero.wallet.data.DefaultNodes;
import net.mynero.wallet.data.Node;
import net.mynero.wallet.service.PrefService;
import net.mynero.wallet.util.Constants;
import java.util.ArrayList;
import java.util.List;
@ -142,7 +141,7 @@ public class NodeSelectionAdapter extends RecyclerView.Adapter<NodeSelectionAdap
private boolean isDefaultNode(Node currentNode) {
boolean isDefault = false;
for(DefaultNodes defaultNode : DefaultNodes.values()) {
if(currentNode.toNodeString().equals(defaultNode.getUri()))
if(currentNode.toNodeString().equals(defaultNode.getNodeString()))
isDefault = true;
}

View file

@ -18,36 +18,65 @@ package net.mynero.wallet.data;
// Nodes stolen from https://moneroworld.com/#nodes
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import timber.log.Timber;
public enum DefaultNodes {
SAMOURAI("163.172.56.213:18089/mainnet/SamouraiWallet"),
MONERUJO("nodex.monerujo.io:18081/mainnet/monerujo"),
SUPPORTXMR("node.supportxmr.com:18081/mainnet/SupportXMR"),
HASHVAULT("nodes.hashvault.pro:18081/mainnet/Hashvault"),
MONEROWORLD("node.moneroworld.com:18089/mainnet/MoneroWorld"),
XMRTW("opennode.xmr-tw.org:18089/mainnet/XMRTW"),
MYNERO_I2P("ynk3hrwte23asonojqeskoulek2g2cd6tqg4neghnenfyljrvhga.b32.i2p:0/mainnet/node.mysu.i2p"),
MYNERO_ONION("tiopyrxseconw73thwlv2pf5hebfcqxj5zdolym7z6pbq6gl4z7xz4ad.onion:18081/mainnet/node.mysu.onion"),
SAMOURAI_ONION("446unwib5vc7pfbzflosy6m6vtyuhddnalr3hutyavwe4esfuu5g6ryd.onion:18089/mainnet/SamouraiWallet.onion"),
MONERUJO_ONION("monerujods7mbghwe6cobdr6ujih6c22zu5rl7zshmizz2udf7v7fsad.onion:18081/mainnet/monerujo.onion"),
Criminales78("56wl7y2ebhamkkiza4b7il4mrzwtyvpdym7bm2bkg3jrei2je646k3qd.onion:18089/mainnet/Criminales78.onion"),
xmrfail("mxcd4577fldb3ppzy7obmmhnu3tf57gbcbd4qhwr2kxyjj2qi3dnbfqd.onion:18081/mainnet/xmrfail.onion"),
boldsuck("6dsdenp6vjkvqzy4wzsnzn6wixkdzihx3khiumyzieauxuxslmcaeiad.onion:18081/mainnet/boldsuck.onion");
SAMOURAI("163.172.56.213", 18089, "mainnet", "SamouraiWallet"),
MONERUJO("nodex.monerujo.io", 18081, "mainnet", "monerujo"),
SUPPORTXMR("node.supportxmr.com", 18081, "mainnet", "SupportXMR"),
HASHVAULT("nodes.hashvault.pro", 18081, "mainnet", "Hashvault"),
MONEROWORLD("node.moneroworld.com", 18089, "mainnet", "MoneroWorld"),
XMRTW("opennode.xmr-tw.org", 18089, "mainnet", "XMRTW"),
MYNERO_I2P("ynk3hrwte23asonojqeskoulek2g2cd6tqg4neghnenfyljrvhga.b32.i2p", 0, "mainnet", "node.mysu.i2p"),
MYNERO_ONION("tiopyrxseconw73thwlv2pf5hebfcqxj5zdolym7z6pbq6gl4z7xz4ad.onion", 18081, "mainnet", "node.mysu.onion"),
SAMOURAI_ONION("446unwib5vc7pfbzflosy6m6vtyuhddnalr3hutyavwe4esfuu5g6ryd.onion", 18089, "mainnet", "SamouraiWallet.onion"),
MONERUJO_ONION("monerujods7mbghwe6cobdr6ujih6c22zu5rl7zshmizz2udf7v7fsad.onion", 18081, "mainnet", "monerujo.onion"),
Criminales78("56wl7y2ebhamkkiza4b7il4mrzwtyvpdym7bm2bkg3jrei2je646k3qd.onion", 18089, "mainnet", "Criminales78.onion"),
xmrfail("mxcd4577fldb3ppzy7obmmhnu3tf57gbcbd4qhwr2kxyjj2qi3dnbfqd.onion", 18081, "mainnet", "xmrfail.onion"),
boldsuck("6dsdenp6vjkvqzy4wzsnzn6wixkdzihx3khiumyzieauxuxslmcaeiad.onion", 18081, "mainnet", "boldsuck.onion");
private final String uri;
private final String address;
private final int port;
private final String network;
private final String name;
DefaultNodes(String uri) {
this.uri = uri;
DefaultNodes(String address, int port, String network, String name) {
this.address = address;
this.port = port;
this.network = network;
this.name = name;
}
public String getUri() {
return uri;
public String getNodeString() {
return address + ":" + port + "/" + network + "/" + name;
}
public JSONObject getJson() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("host", address);
jsonObject.put("rpcPort", port);
jsonObject.put("network", network);
if (!name.isEmpty())
jsonObject.put("name", name);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return jsonObject;
}
public String getAddress() {
return uri.split("/")[0];
return address;
}
public String getName() {
return uri.split("/")[2];
return name;
}
}

View file

@ -20,6 +20,9 @@ import net.mynero.wallet.model.NetworkType;
import net.mynero.wallet.model.WalletManager;
import net.mynero.wallet.util.OnionHelper;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
@ -34,7 +37,7 @@ public class Node {
static public final String TESTNET = "testnet";
static private int DEFAULT_LEVIN_PORT = 0;
static private int DEFAULT_RPC_PORT = 0;
final private NetworkType networkType;
private NetworkType networkType;
private final boolean selected = false;
int rpcPort = 0;
private String name = null;
@ -144,6 +147,47 @@ public class Node {
this.levinPort = getDefaultLevinPort();
}
Node(JSONObject jsonObject) throws JSONException, UnknownHostException {
if (jsonObject == null)
throw new IllegalArgumentException("daemon is empty");
if(jsonObject.has("username")) {
username = jsonObject.getString("username");
}
if(jsonObject.has("password")) {
password = jsonObject.getString("password");
}
if(jsonObject.has("host")) {
setHost(jsonObject.getString("host"));
}
if(jsonObject.has("rpcPort")) {
this.rpcPort = jsonObject.getInt("rpcPort");
} else {
this.rpcPort = getDefaultRpcPort();
}
if(jsonObject.has("name")) {
this.name = jsonObject.getString("name");
}
if(jsonObject.has("network")) {
switch (jsonObject.getString("network")) {
case MAINNET:
networkType = NetworkType.NetworkType_Mainnet;
break;
case STAGENET:
networkType = NetworkType.NetworkType_Stagenet;
break;
case TESTNET:
networkType = NetworkType.NetworkType_Testnet;
break;
default:
throw new IllegalArgumentException("invalid net: " + jsonObject.getString("network"));
}
if (networkType != WalletManager.getInstance().getNetworkType())
throw new IllegalArgumentException("wrong net: " + networkType);
}
}
public Node() {
this.networkType = WalletManager.getInstance().getNetworkType();
}
@ -172,6 +216,15 @@ public class Node {
}
}
static public Node fromJson(JSONObject jsonObject) {
try {
return new Node(jsonObject);
} catch (IllegalArgumentException | UnknownHostException | JSONException ex) {
Timber.w(ex);
return null;
}
}
// every node knows its network, but they are all the same
static public int getDefaultLevinPort() {
if (DEFAULT_LEVIN_PORT > 0) return DEFAULT_LEVIN_PORT;
@ -240,6 +293,35 @@ public class Node {
return toString();
}
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
try {
if (!username.isEmpty() && !password.isEmpty()) {
jsonObject.put("username", username);
jsonObject.put("password", password);
}
jsonObject.put("host", host);
jsonObject.put("rpcPort", rpcPort);
switch (networkType) {
case NetworkType_Mainnet:
jsonObject.put("network", MAINNET);
break;
case NetworkType_Stagenet:
jsonObject.put("network", STAGENET);
break;
case NetworkType_Testnet:
jsonObject.put("network", TESTNET);
break;
}
if (!name.isEmpty())
jsonObject.put("name", name);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return jsonObject;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

View file

@ -26,6 +26,7 @@ import net.mynero.wallet.util.Helper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class AddNodeBottomSheetDialog extends BottomSheetDialogFragment {
public AddNodeListener listener = null;
@ -70,64 +71,56 @@ public class AddNodeBottomSheetDialog extends BottomSheetDialogFragment {
String name = nodeNameEditText.getText().toString();
String user = usernameEditText.getText().toString();
String pass = passwordEditText.getText().toString();
if(name.isEmpty()) {
Toast.makeText(getContext(), "Enter node name", Toast.LENGTH_SHORT).show();
return;
} else if(node.isEmpty()) {
Toast.makeText(getContext(), "Enter node address", Toast.LENGTH_SHORT).show();
return;
} else if(!user.isEmpty() && pass.isEmpty()) {
Toast.makeText(getContext(), "Enter password", Toast.LENGTH_SHORT).show();
return;
}
JSONObject jsonObject = new JSONObject();
try {
String auth = "";
if(!user.isEmpty() && !pass.isEmpty()) {
auth = user + ":" + pass + "@";
} else if(!user.isEmpty()) {
Toast.makeText(getContext(), "Enter password", Toast.LENGTH_SHORT).show();
return;
if (!user.isEmpty() && !pass.isEmpty()) {
jsonObject.put("username", user);
jsonObject.put("password", pass);
}
String[] nodeParts = node.split(":");
StringBuilder nodeStringBuilder = new StringBuilder();
nodeStringBuilder.append(auth);
jsonObject.put("host", nodeParts[0]);
jsonObject.put("rpcPort", nodeParts[1]);
jsonObject.put("network", "mainnet");
jsonObject.put("name", name);
if(!name.isEmpty()) {
if(!node.isEmpty()) {
if (node.contains(":")) {
String[] nodeParts = node.split(":");
if (nodeParts.length == 2) {
String address = nodeParts[0];
int port = Integer.parseInt(nodeParts[1]);
nodeStringBuilder.append(address).append(":").append(port);
}
} else {
nodeStringBuilder.append(node);
}
nodeStringBuilder.append("/mainnet/").append(name);
} else {
Toast.makeText(getContext(), "Enter node address", Toast.LENGTH_SHORT).show();
return;
}
} else {
Toast.makeText(getContext(), "Enter node name", Toast.LENGTH_SHORT).show();
return;
}
addNodeToSaved(nodeStringBuilder);
addNodeToSaved(jsonObject);
if (listener != null) {
listener.onNodeAdded();
}
} catch (NumberFormatException | JSONException e) {
e.printStackTrace();
} catch (JSONException e) {
throw new RuntimeException(e);
}
});
}
private void addNodeToSaved(StringBuilder nodeStringBuilder) throws JSONException {
private void addNodeToSaved(JSONObject newNodeJsonObject) throws JSONException {
Node newNode = Node.fromJson(newNodeJsonObject);
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = new JSONArray(nodesArray);
boolean exists = false;
for (int i = 0; i < jsonArray.length(); i++) {
String nodeString = jsonArray.getString(i);
if (nodeString.equals(nodeStringBuilder.toString()))
JSONObject nodeJsonObject = jsonArray.getJSONObject(i);
Node node = Node.fromJson(nodeJsonObject);
if (node.toNodeString().equals(newNode.toNodeString()))
exists = true;
}
if (!exists) {
jsonArray.put(nodeStringBuilder.toString());
jsonArray.put(newNodeJsonObject);
} else {
Toast.makeText(getContext(), "Node already exists", Toast.LENGTH_SHORT).show();
return;

View file

@ -26,10 +26,16 @@ import net.mynero.wallet.util.Helper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import timber.log.Timber;
public class EditNodeBottomSheetDialog extends BottomSheetDialogFragment {
public EditNodeListener listener = null;
public String nodeString = "";
public JSONObject nodeJson = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -47,7 +53,7 @@ public class EditNodeBottomSheetDialog extends BottomSheetDialogFragment {
EditText passwordEditText = view.findViewById(R.id.password_edittext);
ImageButton pastePasswordImageButton = view.findViewById(R.id.paste_password_imagebutton);
Node node = Node.fromString(nodeString);
Node node = Node.fromJson(nodeJson);
addressEditText.setText(node.getAddress());
nodeNameEditText.setText(node.getName());
usernameEditText.setText(node.getUsername());
@ -78,7 +84,7 @@ public class EditNodeBottomSheetDialog extends BottomSheetDialogFragment {
addPasteListener(view, passwordEditText, R.id.paste_password_imagebutton);
deleteNodeButton.setOnClickListener(view1 -> {
listener.onNodeDeleted(Node.fromString(nodeString));
listener.onNodeDeleted(Node.fromJson(nodeJson));
dismiss();
});
doneEditingButton.setOnClickListener(view1 -> {
@ -87,39 +93,32 @@ public class EditNodeBottomSheetDialog extends BottomSheetDialogFragment {
String user = usernameEditText.getText().toString();
String pass = passwordEditText.getText().toString();
String auth = "";
if(!user.isEmpty() && !pass.isEmpty()) {
auth = user + ":" + pass + "@";
} else if(!user.isEmpty()) {
if(nodeName.isEmpty()) {
Toast.makeText(getContext(), "Enter node name", Toast.LENGTH_SHORT).show();
return;
} else if(nodeAddress.isEmpty()) {
Toast.makeText(getContext(), "Enter node address", Toast.LENGTH_SHORT).show();
return;
} else if(!user.isEmpty() && pass.isEmpty()) {
Toast.makeText(getContext(), "Enter password", Toast.LENGTH_SHORT).show();
return;
}
StringBuilder nodeStringBuilder = new StringBuilder();
nodeStringBuilder.append(auth);
if(!nodeName.isEmpty()) {
if(!nodeAddress.isEmpty()) {
if (nodeAddress.contains(":")) {
String[] nodeParts = nodeAddress.split(":");
if (nodeParts.length == 2) {
String address = nodeParts[0];
int port = Integer.parseInt(nodeParts[1]);
nodeStringBuilder.append(address).append(":").append(port);
}
} else {
nodeStringBuilder.append(nodeAddress);
}
nodeStringBuilder.append("/mainnet/").append(nodeName);
listener.onNodeEdited(Node.fromString(nodeString), Node.fromString(nodeStringBuilder.toString()));
} else {
Toast.makeText(getContext(), "Enter node address", Toast.LENGTH_SHORT).show();
return;
JSONObject jsonObject = new JSONObject();
try {
if (!user.isEmpty() && !pass.isEmpty()) {
jsonObject.put("username", user);
jsonObject.put("password", pass);
}
} else {
Toast.makeText(getContext(), "Enter node name", Toast.LENGTH_SHORT).show();
return;
String[] nodeParts = nodeAddress.split(":");
jsonObject.put("host", nodeParts[0]);
jsonObject.put("rpcPort", nodeParts[1]);
jsonObject.put("network", "mainnet");
jsonObject.put("name", nodeName);
listener.onNodeEdited(Node.fromJson(nodeJson), Node.fromJson(jsonObject));
} catch (JSONException e) {
throw new RuntimeException(e);
}
dismiss();
});

View file

@ -25,6 +25,7 @@ import net.mynero.wallet.util.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
@ -55,22 +56,40 @@ public class NodeSelectionBottomSheetDialog extends BottomSheetDialogFragment im
dismiss();
});
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = null;
try {
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = new JSONArray(nodesArray);
for (int i = 0; i < jsonArray.length(); i++) {
String nodeString = jsonArray.getString(i);
Node node = Node.fromString(nodeString);
if (node != null) {
nodes.add(node);
jsonArray = new JSONArray(nodesArray);
} catch (JSONException e) { throw new RuntimeException(e); }
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject nodeJsonObject = null;
try {
nodeJsonObject = jsonArray.getJSONObject(i);
if (nodeJsonObject != null) {
Node node = Node.fromJson(nodeJsonObject);
if (node != null) {
nodes.add(node);
}
}
} catch (JSONException e) {
try {
String nodeString = jsonArray.getString(i);
Node node = Node.fromString(nodeString);
if (node != null) {
nodes.add(node);
jsonArray.put(i, node.toJson().toString());
}
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
PrefService.getInstance().edit().putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString()).apply();
for (DefaultNodes defaultNode : DefaultNodes.values()) {
nodes.add(Node.fromString(defaultNode.getUri()));
nodes.add(Node.fromJson(defaultNode.getJson()));
}
adapter.submitList(nodes);
}
@ -83,7 +102,7 @@ public class NodeSelectionBottomSheetDialog extends BottomSheetDialogFragment im
Toast.makeText(activity, getString(R.string.node_selected), Toast.LENGTH_SHORT).show();
});
}
PrefService.getInstance().edit().putString(Constants.PREF_NODE_2, node.toNodeString()).apply();
PrefService.getInstance().edit().putString(Constants.PREF_NODE_2, node.toJson().toString()).apply();
WalletManager.getInstance().setDaemon(node);
adapter.updateSelectedNode();
listener.onNodeSelected();
@ -92,7 +111,7 @@ public class NodeSelectionBottomSheetDialog extends BottomSheetDialogFragment im
@Override
public boolean onSelectEditNode(Node node) {
if (listener != null) {
listener.onClickedEditNode(node.toNodeString());
listener.onClickedEditNode(node.toJson());
}
dismiss();
return true;
@ -100,7 +119,7 @@ public class NodeSelectionBottomSheetDialog extends BottomSheetDialogFragment im
public interface NodeSelectionDialogListener {
void onNodeSelected();
void onClickedEditNode(String nodeString);
void onClickedEditNode(JSONObject nodeJson);
void onClickedAddNode();
}
}

View file

@ -36,6 +36,8 @@ import net.mynero.wallet.service.PrefService;
import net.mynero.wallet.util.Constants;
import net.mynero.wallet.util.RestoreHeight;
import org.json.JSONObject;
import java.io.File;
import java.util.Calendar;
@ -269,7 +271,7 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS
}
@Override
public void onClickedEditNode(String nodeString) { }
public void onClickedEditNode(JSONObject nodeJson) { }
@Override
public void onClickedAddNode() {

View file

@ -42,6 +42,7 @@ import net.mynero.wallet.util.DayNightMode;
import net.mynero.wallet.util.NightmodeHelper;
import org.json.JSONArray;
import org.json.JSONObject;
public class SettingsFragment extends Fragment implements PasswordBottomSheetDialog.PasswordListener, NodeSelectionBottomSheetDialog.NodeSelectionDialogListener, AddNodeBottomSheetDialog.AddNodeListener, EditNodeBottomSheetDialog.EditNodeListener {
@ -247,10 +248,10 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
}
@Override
public void onClickedEditNode(String nodeString) {
public void onClickedEditNode(JSONObject nodeJson) {
EditNodeBottomSheetDialog editNodeDialog = new EditNodeBottomSheetDialog();
editNodeDialog.listener = this;
editNodeDialog.nodeString = nodeString;
editNodeDialog.nodeJson = nodeJson;
editNodeDialog.show(getActivity().getSupportFragmentManager(), "edit_node_dialog");
}
@ -279,8 +280,8 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = new JSONArray(nodesArray);
for (int i = 0; i < jsonArray.length(); i++) {
String jsonNodeString = jsonArray.getString(i);
Node savedNode = Node.fromString(jsonNodeString);
JSONObject nodeJsonObject = jsonArray.getJSONObject(i);
Node savedNode = Node.fromJson(nodeJsonObject);
if (savedNode.toNodeString().equals(node.toNodeString()))
jsonArray.remove(i);
}
@ -296,8 +297,8 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = new JSONArray(nodesArray);
for (int i = 0; i < jsonArray.length(); i++) {
String jsonNodeString = jsonArray.getString(i);
Node savedNode = Node.fromString(jsonNodeString);
JSONObject nodeJsonObject = jsonArray.getJSONObject(i);
Node savedNode = Node.fromJson(nodeJsonObject);
if (savedNode.toNodeString().equals(oldNode.toNodeString()))
jsonArray.put(i, newNode.toNodeString());
}

View file

@ -64,15 +64,14 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
public void run() {
PrefService prefService = PrefService.getInstance();
boolean usesTor = prefService.getBoolean(Constants.PREF_USES_TOR, false);
String currentNodeString = prefService.getNode().toNodeString();
Node selectedNode = Node.fromString(currentNodeString);
boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1");
Node currentNode = prefService.getNode();
boolean isLocalIp = currentNode.getAddress().startsWith("10.") || currentNode.getAddress().startsWith("192.168.") || currentNode.getAddress().equals("localhost") || currentNode.getAddress().equals("127.0.0.1");
if (usesTor && !isLocalIp) {
String proxy = prefService.getProxy();
WalletManager.getInstance().setProxy(proxy);
wallet.setProxy(proxy);
}
WalletManager.getInstance().setDaemon(selectedNode);
WalletManager.getInstance().setDaemon(currentNode);
wallet.init(0);
wallet.setListener(this);
wallet.startRefresh();

View file

@ -10,6 +10,7 @@ import net.mynero.wallet.util.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PrefService extends ServiceBase {
private static SharedPreferences preferences = null;
@ -39,64 +40,20 @@ public class PrefService extends ServiceBase {
}
}
}
String nodeString = getString(Constants.PREF_NODE_2, defaultNode.getUri());
if(!nodeString.isEmpty()) {
return Node.fromString(nodeString);
} else {
return null;
}
}
public void upgradeNodePrefs() {
String oldNodeString = getString("pref_node", "");
if(!oldNodeString.isEmpty()) {
//upgrade old node pref to new node pref
try {
Node oldNode = getNode(oldNodeString);
if(oldNode != null) {
SharedPreferences.Editor editor = edit();
editor.putString(Constants.PREF_NODE_2, oldNode.toNodeString());
editor.putString("pref_node", "");
editor.apply();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private Node getNode(String oldNodeString) throws JSONException {
String nodeString = "";
String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]");
JSONArray jsonArray = new JSONArray(nodesArray);
for (int i = 0; i < jsonArray.length(); i++) {
//check custom nodes
String jsonNodeString = jsonArray.getString(i);
Node savedNode = Node.fromString(jsonNodeString);
if(savedNode != null) {
if (savedNode.getAddress().equals(oldNodeString)) {
nodeString = savedNode.toNodeString();
break;
}
}
}
if(nodeString.isEmpty()) {
//not in custom nodes, maybe in default nodes?
for (DefaultNodes defaultNode : DefaultNodes.values()) {
Node node = Node.fromString(defaultNode.getUri());
String nodeString = getString(Constants.PREF_NODE_2, defaultNode.getNodeString());
try {
JSONObject nodeJson = new JSONObject(nodeString);
return Node.fromJson(nodeJson);
} catch (JSONException e) {
if(!nodeString.isEmpty()) {
Node node = Node.fromString(nodeString);
if(node != null) {
if(node.getAddress().equals(oldNodeString)) {
nodeString = node.toNodeString();
break;
}
edit().putString(Constants.PREF_NODE_2, node.toJson().toString()).apply();
return node;
}
}
}
if(nodeString.isEmpty()) {
return null;
} else {
return Node.fromString(nodeString);
}
return null;
}
public String getProxy() {

View file

@ -5,7 +5,6 @@ public class Constants {
public static final String MNEMONIC_LANGUAGE = "English";
public static final String PREF_USES_PASSWORD = "pref_uses_password";
public static final String PREF_USES_TOR = "pref_uses_tor";
public static final String PREF_NIGHT_MODE = "pref_night_mode";
public static final String PREF_PROXY = "pref_proxy";
public static final String PREF_NODE_2 = "pref_node_2";
public static final String PREF_CUSTOM_NODES = "pref_custom_nodes";