Cleanup default node code

This commit is contained in:
pokkst 2022-10-15 01:05:06 -05:00
parent c6b1d0a4af
commit 90e556abff
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
7 changed files with 75 additions and 55 deletions

View File

@ -1,6 +1,7 @@
package net.mynero.wallet; package net.mynero.wallet;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle; import android.os.PersistableBundle;
@ -108,43 +109,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
} }
private void upgradeOldNodePrefs() { private void upgradeOldNodePrefs() {
try { PrefService.getInstance().getNode();
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();
}
} }
@Override @Override

View File

@ -96,10 +96,7 @@ public class NodeSelectionAdapter extends RecyclerView.Adapter<NodeSelectionAdap
} }
public void bind(Node node) { public void bind(Node node) {
boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); Node currentNode = PrefService.getInstance().getNode();
DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI;
String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri());
Node currentNode = Node.fromString(currentNodeString);
boolean match = node.equals(currentNode); boolean match = node.equals(currentNode);
if (match) { if (match) {
itemView.setBackgroundColor(itemView.getResources().getColor(R.color.oled_colorSecondary)); itemView.setBackgroundColor(itemView.getResources().getColor(R.color.oled_colorSecondary));

View File

@ -159,9 +159,7 @@ public class OnboardingFragment extends Fragment {
} }
private void prepareDefaultNode() { private void prepareDefaultNode() {
boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); PrefService.getInstance().getNode();
DefaultNodes defaultNode = usesTor ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI;
PrefService.getInstance().edit().putString(Constants.PREF_NODE_2, defaultNode.getUri()).apply();
} }
private void createOrImportWallet(String walletPassword, String walletSeed, String restoreHeightText) { private void createOrImportWallet(String walletPassword, String walletSeed, String restoreHeightText) {

View File

@ -174,8 +174,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
statusTextView.setText(getResources().getText(R.string.version_mismatch)); statusTextView.setText(getResources().getText(R.string.version_mismatch));
} }
}); });
DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; Node node = PrefService.getInstance().getNode(); // shouldn't use default value here
Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri())); // shouldn't use default value here
selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress()));
selectNodeButton.setOnClickListener(view1 -> { selectNodeButton.setOnClickListener(view1 -> {
NodeSelectionBottomSheetDialog dialog = new NodeSelectionBottomSheetDialog(); NodeSelectionBottomSheetDialog dialog = new NodeSelectionBottomSheetDialog();
@ -221,9 +220,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
@Override @Override
public void onNodeSelected() { public void onNodeSelected() {
boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); Node node = PrefService.getInstance().getNode();
DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI;
Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri()));
selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress()));
mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication()));
((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> {

View File

@ -18,8 +18,7 @@ public class SettingsViewModel extends ViewModel {
public void updateProxy(MoneroApplication application) { public void updateProxy(MoneroApplication application) {
application.getExecutor().execute(() -> { application.getExecutor().execute(() -> {
boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false);
DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; String currentNodeString = PrefService.getInstance().getNode().toNodeString();
String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri());
boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1");
if (!usesProxy || isNodeLocalIp) { if (!usesProxy || isNodeLocalIp) {

View File

@ -57,8 +57,7 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
@Override @Override
public void run() { public void run() {
boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false);
DefaultNodes defaultNode = usesTor ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; String currentNodeString = PrefService.getInstance().getNode().toNodeString();
String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri());
Node selectedNode = Node.fromString(currentNodeString); Node selectedNode = Node.fromString(currentNodeString);
boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1");
if (usesTor && !isLocalIp) { if (usesTor && !isLocalIp) {

View File

@ -4,10 +4,16 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import net.mynero.wallet.MoneroApplication; 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 class PrefService extends ServiceBase {
public static SharedPreferences preferences = null; private static SharedPreferences preferences = null;
public static PrefService instance = null; private static PrefService instance = null;
public PrefService(MoneroApplication application) { public PrefService(MoneroApplication application) {
super(null); super(null);
@ -19,6 +25,65 @@ public class PrefService extends ServiceBase {
return preferences.edit(); 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) { public String getString(String key, String defaultValue) {
String value = preferences.getString(key, ""); String value = preferences.getString(key, "");
if(value.isEmpty() && !defaultValue.isEmpty()) { if(value.isEmpty() && !defaultValue.isEmpty()) {