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;
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

View file

@ -96,10 +96,7 @@ public class NodeSelectionAdapter extends RecyclerView.Adapter<NodeSelectionAdap
}
public void bind(Node node) {
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());
Node currentNode = Node.fromString(currentNodeString);
Node currentNode = PrefService.getInstance().getNode();
boolean match = node.equals(currentNode);
if (match) {
itemView.setBackgroundColor(itemView.getResources().getColor(R.color.oled_colorSecondary));

View file

@ -159,9 +159,7 @@ public class OnboardingFragment extends Fragment {
}
private void prepareDefaultNode() {
boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false);
DefaultNodes defaultNode = usesTor ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI;
PrefService.getInstance().edit().putString(Constants.PREF_NODE_2, defaultNode.getUri()).apply();
PrefService.getInstance().getNode();
}
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));
}
});
DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI;
Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE_2, defaultNode.getUri())); // shouldn't use default value here
Node node = PrefService.getInstance().getNode(); // shouldn't use default value here
selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress()));
selectNodeButton.setOnClickListener(view1 -> {
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(() -> {

View file

@ -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) {

View file

@ -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) {

View file

@ -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()) {