diff --git a/app/src/main/java/net/mynero/wallet/MainActivity.java b/app/src/main/java/net/mynero/wallet/MainActivity.java index 9886403..856fc05 100644 --- a/app/src/main/java/net/mynero/wallet/MainActivity.java +++ b/app/src/main/java/net/mynero/wallet/MainActivity.java @@ -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(); 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 72381a1..23add8d 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java @@ -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 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(); diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java index a0a1d58..7a8e37c 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java @@ -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; diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/EditNodeBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/EditNodeBottomSheetDialog.java index 2aac51b..47af12f 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/EditNodeBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/EditNodeBottomSheetDialog.java @@ -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(); }); diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/NodeSelectionBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/NodeSelectionBottomSheetDialog.java index 046c5b6..c32b08e 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/NodeSelectionBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/NodeSelectionBottomSheetDialog.java @@ -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(); } } \ No newline at end of file diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java index 0c13407..8c4c79e 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java @@ -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() { diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java index 3cacbef..cfebaf9 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java @@ -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()); } 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 9969259..85dd5a7 100644 --- a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java +++ b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java @@ -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(); 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 b956c59..d19ebd2 100644 --- a/app/src/main/java/net/mynero/wallet/service/PrefService.java +++ b/app/src/main/java/net/mynero/wallet/service/PrefService.java @@ -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() { diff --git a/app/src/main/java/net/mynero/wallet/util/Constants.java b/app/src/main/java/net/mynero/wallet/util/Constants.java index 6320e73..5dbcc85 100644 --- a/app/src/main/java/net/mynero/wallet/util/Constants.java +++ b/app/src/main/java/net/mynero/wallet/util/Constants.java @@ -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";