From 90e556abff858973a878e078c4330b17bc2df0e9 Mon Sep 17 00:00:00 2001 From: pokkst Date: Sat, 15 Oct 2022 01:05:06 -0500 Subject: [PATCH] Cleanup default node code --- .../java/net/mynero/wallet/MainActivity.java | 39 +---------- .../wallet/adapter/NodeSelectionAdapter.java | 5 +- .../onboarding/OnboardingFragment.java | 4 +- .../fragment/settings/SettingsFragment.java | 7 +- .../fragment/settings/SettingsViewModel.java | 3 +- .../wallet/service/MoneroHandlerThread.java | 3 +- .../mynero/wallet/service/PrefService.java | 69 ++++++++++++++++++- 7 files changed, 75 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/MainActivity.java b/app/src/main/java/net/mynero/wallet/MainActivity.java index 6f99263..3d9bd29 100644 --- a/app/src/main/java/net/mynero/wallet/MainActivity.java +++ b/app/src/main/java/net/mynero/wallet/MainActivity.java @@ -1,6 +1,7 @@ package net.mynero.wallet; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.os.PersistableBundle; @@ -108,43 +109,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre } private void upgradeOldNodePrefs() { - try { - String oldNodeString = PrefService.getInstance().getString("pref_node", ""); - String nodeString = ""; - if (!oldNodeString.isEmpty()) { - 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); - if(savedNode != null) { - if (savedNode.getAddress().equals(oldNodeString)) { - nodeString = savedNode.toNodeString(); - break; - } - } - } - if(nodeString.isEmpty()) { - for (DefaultNodes defaultNode : DefaultNodes.values()) { - Node node = Node.fromString(defaultNode.getUri()); - if(node != null) { - if(node.getAddress().equals(oldNodeString)) { - nodeString = node.toNodeString(); - break; - } - } - } - } - if(!nodeString.isEmpty()) { - Node oldNode = Node.fromString(nodeString); - if (oldNode != null) { - PrefService.getInstance().edit().putString(Constants.PREF_NODE_2, oldNode.toNodeString()).apply(); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } + PrefService.getInstance().getNode(); } @Override diff --git a/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java b/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java index 9813dd3..999bd8d 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java @@ -96,10 +96,7 @@ public class NodeSelectionAdapter extends RecyclerView.Adapter { NodeSelectionBottomSheetDialog dialog = new NodeSelectionBottomSheetDialog(); @@ -221,9 +220,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia @Override public void onNodeSelected() { - boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; - Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri())); + Node node = PrefService.getInstance().getNode(); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java index 5ef4eab..9d1342a 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java @@ -18,8 +18,7 @@ public class SettingsViewModel extends ViewModel { public void updateProxy(MoneroApplication application) { application.getExecutor().execute(() -> { boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; - String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri()); + String currentNodeString = PrefService.getInstance().getNode().toNodeString(); boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); if (!usesProxy || isNodeLocalIp) { diff --git a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java index a988bd1..9b23161 100644 --- a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java +++ b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java @@ -57,8 +57,7 @@ public class MoneroHandlerThread extends Thread implements WalletListener { @Override public void run() { boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - DefaultNodes defaultNode = usesTor ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; - String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri()); + String currentNodeString = PrefService.getInstance().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"); if (usesTor && !isLocalIp) { diff --git a/app/src/main/java/net/mynero/wallet/service/PrefService.java b/app/src/main/java/net/mynero/wallet/service/PrefService.java index 5116fc2..c72c88a 100644 --- a/app/src/main/java/net/mynero/wallet/service/PrefService.java +++ b/app/src/main/java/net/mynero/wallet/service/PrefService.java @@ -4,10 +4,16 @@ import android.content.Context; import android.content.SharedPreferences; import net.mynero.wallet.MoneroApplication; +import net.mynero.wallet.data.DefaultNodes; +import net.mynero.wallet.data.Node; +import net.mynero.wallet.util.Constants; + +import org.json.JSONArray; +import org.json.JSONException; public class PrefService extends ServiceBase { - public static SharedPreferences preferences = null; - public static PrefService instance = null; + private static SharedPreferences preferences = null; + private static PrefService instance = null; public PrefService(MoneroApplication application) { super(null); @@ -19,6 +25,65 @@ public class PrefService extends ServiceBase { return preferences.edit(); } + public Node getNode() { + 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(); + } + } + boolean usesProxy = getBoolean(Constants.PREF_USES_TOR, false); + DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; + String nodeString = getString(Constants.PREF_NODE_2, defaultNode.getUri()); + if(!nodeString.isEmpty()) { + return Node.fromString(nodeString); + } else { + return null; + } + } + + 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++) { + 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()) { + for (DefaultNodes defaultNode : DefaultNodes.values()) { + Node node = Node.fromString(defaultNode.getUri()); + if(node != null) { + if(node.getAddress().equals(oldNodeString)) { + nodeString = node.toNodeString(); + break; + } + } + } + } + if(nodeString.isEmpty()) { + return null; + } else { + Node oldNode = Node.fromString(nodeString); + return oldNode; + } + } + public String getString(String key, String defaultValue) { String value = preferences.getString(key, ""); if(value.isEmpty() && !defaultValue.isEmpty()) {