deamon selection belongs to loginfragment (#74)

This commit is contained in:
m2049r 2017-09-20 19:43:59 +02:00 committed by GitHub
parent b00734a0d6
commit b78902ac77
4 changed files with 184 additions and 179 deletions

View File

@ -73,9 +73,6 @@ public class LoginActivity extends AppCompatActivity
static final int DAEMON_TIMEOUT = 500; // deamon must respond in 500ms
Toolbar toolbar;
EditText etDummy;
DropDownEditText etDaemonAddress;
ArrayAdapter<String> nodeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -88,45 +85,6 @@ public class LoginActivity extends AppCompatActivity
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etDummy = (EditText) findViewById(R.id.etDummy);
etDaemonAddress = (DropDownEditText) findViewById(R.id.etDaemonAddress);
nodeAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line);
etDaemonAddress.setAdapter(nodeAdapter);
Helper.hideKeyboard(this);
etDaemonAddress.setThreshold(0);
etDaemonAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etDaemonAddress.showDropDown();
Helper.showKeyboard(LoginActivity.this);
}
});
etDaemonAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
etDaemonAddress.showDropDown();
Helper.showKeyboard(LoginActivity.this);
}
}
});
etDaemonAddress.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Helper.hideKeyboard(LoginActivity.this);
etDummy.requestFocus();
return true;
}
return false;
}
});
loadPrefs();
if (Helper.getWritePermission(this)) {
startLoginFragment();
} else {
@ -144,24 +102,18 @@ public class LoginActivity extends AppCompatActivity
}
@Override
public void onWalletSelected(final String walletName) {
String daemon = getDaemon();
public boolean onWalletSelected(String daemon, final String walletName, boolean testnet) {
if (daemon.length() == 0) {
Toast.makeText(this, getString(R.string.prompt_daemon_missing), Toast.LENGTH_SHORT).show();
etDaemonAddress.requestFocus();
Helper.showKeyboard(this);
return;
return false;
}
if (!checkAndSetWalletDaemon(getDaemon(), isTestnet())) {
if (!checkAndSetWalletDaemon(daemon, testnet)) {
Toast.makeText(this, getString(R.string.warn_daemon_unavailable), Toast.LENGTH_SHORT).show();
return;
return false;
}
// looking good
savePrefs();
if (checkServiceRunning()) return;
if (checkServiceRunning()) return true;
Log.d(TAG, "selected wallet is ." + walletName + ".");
// now it's getting real, check if wallet exists
File walletFile = Helper.getWalletFile(this, walletName);
@ -175,11 +127,12 @@ public class LoginActivity extends AppCompatActivity
} else { // this cannot really happen as we prefilter choices
Toast.makeText(this, getString(R.string.bad_wallet), Toast.LENGTH_SHORT).show();
}
return true;
}
@Override
public void onWalletDetails(final String walletName) {
checkAndSetWalletDaemon("", isTestnet()); // just set selected net
public void onWalletDetails(final String walletName, boolean testnet) {
checkAndSetWalletDaemon("", testnet); // just set selected net
Log.d(TAG, "details for wallet ." + walletName + ".");
if (checkServiceRunning()) return;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@ -216,8 +169,8 @@ public class LoginActivity extends AppCompatActivity
}
@Override
public void onWalletReceive(String walletName) {
checkAndSetWalletDaemon("", isTestnet()); // just set selected net
public void onWalletReceive(String walletName, boolean testnet) {
checkAndSetWalletDaemon("", testnet); // just set selected net
Log.d(TAG, "receive for wallet ." + walletName + ".");
if (checkServiceRunning()) return;
final File walletFile = Helper.getWalletFile(this, walletName);
@ -440,6 +393,7 @@ public class LoginActivity extends AppCompatActivity
}
void reloadWalletList() {
Log.d(TAG, "reloadWalletList()");
try {
LoginFragment loginFragment = (LoginFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container);
@ -451,8 +405,8 @@ public class LoginActivity extends AppCompatActivity
}
@Override
public void onAddWallet() {
checkAndSetWalletDaemon("", isTestnet());
public void onAddWallet(boolean testnet) {
checkAndSetWalletDaemon("", testnet);
if (checkServiceRunning()) return;
startGenerateFragment();
}
@ -558,6 +512,7 @@ public class LoginActivity extends AppCompatActivity
toolbar.setSubtitle(subtitle);
}
@Override
public void showNet(boolean testnet) {
if (testnet) {
toolbar.setBackgroundResource(R.color.colorPrimaryDark);
@ -570,7 +525,6 @@ public class LoginActivity extends AppCompatActivity
@Override
protected void onPause() {
Log.d(TAG, "onPause()");
savePrefs();
super.onPause();
}
@ -991,13 +945,6 @@ public class LoginActivity extends AppCompatActivity
}
}
boolean testnet = false;
@Override
public boolean isTestnet() {
return testnet;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
@ -1005,92 +952,18 @@ public class LoginActivity extends AppCompatActivity
LicensesFragment.displayLicensesFragment(getSupportFragmentManager());
return true;
case R.id.action_testnet:
boolean lastState = testnet;//item.isChecked();
item.setChecked(!lastState);
setNet(!lastState, true); // set and save
try {
LoginFragment loginFragment = (LoginFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container);
item.setChecked(loginFragment.onTestnetMenuItem());
} catch (ClassCastException ex) {
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void setNet(boolean testnet, boolean save) {
this.testnet = testnet;
showNet(testnet);
if (save) {
savePrefs(true); // use previous state as we just clicked it
}
if (testnet) {
setDaemon(daemonTestNet);
} else {
setDaemon(daemonMainNet);
}
etDummy.requestFocus();
Helper.hideKeyboard(this);
reloadWalletList();
}
String getDaemon() {
return etDaemonAddress.getText().toString();
}
void setDaemon(NodeList nodeList) {
Log.d(TAG, "setDaemon() " + nodeList.toString());
String[] nodes = nodeList.getNodes().toArray(new String[0]);
nodeAdapter.clear();
nodeAdapter.addAll(nodes);
etDaemonAddress.getText().clear();
if (nodes.length > 0) {
etDaemonAddress.setText(nodes[0]);
}
etDaemonAddress.dismissDropDown();
}
private static final String PREF_DAEMON_TESTNET = "daemon_testnet";
private static final String PREF_DAEMON_MAINNET = "daemon_mainnet";
private static final String PREF_TESTNET = "testnet";
private static final String PREF_DAEMONLIST_MAINNET =
"node.moneroworld.com:18089;node.xmrbackb.one:18081;node.xmr.be:18081";
private NodeList daemonTestNet;
private NodeList daemonMainNet;
void loadPrefs() {
SharedPreferences sharedPref = getPrefs();
boolean testnet = sharedPref.getBoolean(PREF_TESTNET, false);
daemonMainNet = new NodeList(sharedPref.getString(PREF_DAEMON_MAINNET, PREF_DAEMONLIST_MAINNET));
daemonTestNet = new NodeList(sharedPref.getString(PREF_DAEMON_TESTNET, ""));
//############### ignore last net state - always start in mainnet (like other clients)
// MenuItem net = (MenuItem) getView().findViewById(R.id.action_testnet);
// net.setChecked(!mainnet);
setNet(testnet, false);
}
void savePrefs() {
savePrefs(false);
}
void savePrefs(boolean usePreviousState) {
Log.d(TAG, "SAVE / " + usePreviousState);
// save the daemon address for the net
boolean testnet = isTestnet() ^ usePreviousState;
String daemon = getDaemon();
if (testnet) {
daemonTestNet.setRecent(daemon);
} else {
daemonMainNet.setRecent(daemon);
}
SharedPreferences sharedPref = getPrefs();
SharedPreferences.Editor editor = sharedPref.edit();
//editor.putBoolean(PREF_TESTNET, testnet);
editor.putString(PREF_DAEMON_MAINNET, daemonMainNet.toString());
editor.putString(PREF_DAEMON_TESTNET, daemonTestNet.toString());
editor.apply();
}
private boolean checkAndSetWalletDaemon(String daemon, boolean testnet) {
String daemonAddress = "";
String username = "";

View File

@ -25,21 +25,26 @@ import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.m2049r.xmrwallet.layout.DropDownEditText;
import com.m2049r.xmrwallet.model.WalletManager;
import com.m2049r.xmrwallet.util.Helper;
import com.m2049r.xmrwallet.util.NodeList;
import java.io.File;
import java.util.ArrayList;
@ -64,6 +69,9 @@ public class LoginFragment extends Fragment {
});
List<String> displayedList = new ArrayList<>();
EditText etDummy;
DropDownEditText etDaemonAddress;
ArrayAdapter<String> nodeAdapter;
FloatingActionButton fabAdd;
Listener activityCallback;
@ -74,11 +82,11 @@ public class LoginFragment extends Fragment {
File getStorageRoot();
void onWalletSelected(String wallet);
boolean onWalletSelected(String daemon, String wallet, boolean testnet);
void onWalletDetails(String wallet);
void onWalletDetails(String wallet, boolean testnet);
void onWalletReceive(String wallet);
void onWalletReceive(String wallet, boolean testnet);
void onWalletRename(String name);
@ -86,10 +94,9 @@ public class LoginFragment extends Fragment {
void onWalletArchive(String walletName);
void onAddWallet();
boolean isTestnet();
void onAddWallet(boolean testnet);
void showNet(boolean testnet);
}
@Override
@ -106,6 +113,7 @@ public class LoginFragment extends Fragment {
@Override
public void onPause() {
Log.d(TAG, "onPause()");
savePrefs();
super.onPause();
}
@ -125,7 +133,7 @@ public class LoginFragment extends Fragment {
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
activityCallback.onAddWallet();
activityCallback.onAddWallet(isTestnet());
}
});
@ -145,7 +153,7 @@ public class LoginFragment extends Fragment {
return;
}
String x = activityCallback.isTestnet() ? "9A-" : "4-";
String x = isTestnet() ? "9A-" : "4-";
if (x.indexOf(itemValue.charAt(1)) < 0) {
Toast.makeText(getActivity(), getString(R.string.prompt_wrong_net), Toast.LENGTH_LONG).show();
return;
@ -153,17 +161,56 @@ public class LoginFragment extends Fragment {
String wallet = itemValue.substring(WALLETNAME_PREAMBLE_LENGTH);
activityCallback.onWalletSelected(wallet);
if (activityCallback.onWalletSelected(getDaemon(), wallet, isTestnet())) {
savePrefs();
}
}
});
loadList();
etDummy = (EditText) view.findViewById(R.id.etDummy);
etDaemonAddress = (DropDownEditText) view.findViewById(R.id.etDaemonAddress);
nodeAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line);
etDaemonAddress.setAdapter(nodeAdapter);
Helper.hideKeyboard(getActivity());
etDaemonAddress.setThreshold(0);
etDaemonAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etDaemonAddress.showDropDown();
Helper.showKeyboard(getActivity());
}
});
etDaemonAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
etDaemonAddress.showDropDown();
Helper.showKeyboard(getActivity());
}
}
});
etDaemonAddress.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Helper.hideKeyboard(getActivity());
etDummy.requestFocus();
return true;
}
return false;
}
});
loadPrefs();
return view;
}
private void filterList() {
displayedList.clear();
String x = activityCallback.isTestnet() ? "9A" : "4";
String x = isTestnet() ? "9A" : "4";
for (String s : walletList) {
if (x.indexOf(s.charAt(1)) >= 0) displayedList.add(s);
}
@ -204,7 +251,7 @@ public class LoginFragment extends Fragment {
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String listItem = (String) listView.getItemAtPosition(info.position);
String name = nameFromListItem(listItem, activityCallback.isTestnet());
String name = nameFromListItem(listItem, isTestnet());
if (name == null) {
Toast.makeText(getActivity(), getString(R.string.panic), Toast.LENGTH_LONG).show();
}
@ -231,11 +278,11 @@ public class LoginFragment extends Fragment {
}
private void showInfo(@NonNull String name) {
activityCallback.onWalletDetails(name);
activityCallback.onWalletDetails(name, isTestnet());
}
private boolean showReceive(@NonNull String name) {
activityCallback.onWalletReceive(name);
activityCallback.onWalletReceive(name, isTestnet());
return true;
}
@ -248,7 +295,6 @@ public class LoginFragment extends Fragment {
return wallet;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -258,6 +304,92 @@ public class LoginFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.list_menu, menu);
menu.findItem(R.id.action_testnet).setChecked(isTestnet());
super.onCreateOptionsMenu(menu, inflater);
}
boolean testnet = false;
boolean isTestnet() {
return testnet;
}
public boolean onTestnetMenuItem() {
boolean lastState = testnet;//item.isChecked();
setNet(!lastState, true); // set and save
return !lastState;
}
public void setNet(boolean testnet, boolean save) {
this.testnet = testnet;
activityCallback.showNet(testnet);
if (save) {
savePrefs(true); // use previous state as we just clicked it
}
if (testnet) {
setDaemon(daemonTestNet);
} else {
setDaemon(daemonMainNet);
}
loadList();
}
private static final String PREF_DAEMON_TESTNET = "daemon_testnet";
private static final String PREF_DAEMON_MAINNET = "daemon_mainnet";
//private static final String PREF_TESTNET = "testnet";
private static final String PREF_DAEMONLIST_MAINNET =
"node.moneroworld.com:18089;node.xmrbackb.one:18081;node.xmr.be:18081";
private NodeList daemonTestNet;
private NodeList daemonMainNet;
void loadPrefs() {
SharedPreferences sharedPref = activityCallback.getPrefs();
daemonMainNet = new NodeList(sharedPref.getString(PREF_DAEMON_MAINNET, PREF_DAEMONLIST_MAINNET));
daemonTestNet = new NodeList(sharedPref.getString(PREF_DAEMON_TESTNET, ""));
setNet(isTestnet(), false);
}
void savePrefs() {
savePrefs(false);
}
void savePrefs(boolean usePreviousState) {
Log.d(TAG, "SAVE / " + usePreviousState);
// save the daemon address for the net
boolean testnet = isTestnet() ^ usePreviousState;
String daemon = getDaemon();
if (testnet) {
daemonTestNet.setRecent(daemon);
} else {
daemonMainNet.setRecent(daemon);
}
SharedPreferences sharedPref = activityCallback.getPrefs();
SharedPreferences.Editor editor = sharedPref.edit();
//editor.putBoolean(PREF_TESTNET, testnet);
editor.putString(PREF_DAEMON_MAINNET, daemonMainNet.toString());
editor.putString(PREF_DAEMON_TESTNET, daemonTestNet.toString());
editor.apply();
}
String getDaemon() {
return etDaemonAddress.getText().toString();
}
void setDaemon(NodeList nodeList) {
Log.d(TAG, "setDaemon() " + nodeList.toString());
String[] nodes = nodeList.getNodes().toArray(new String[0]);
nodeAdapter.clear();
nodeAdapter.addAll(nodes);
etDaemonAddress.getText().clear();
if (nodes.length > 0) {
etDaemonAddress.setText(nodes[0]);
}
etDaemonAddress.dismissDropDown();
etDummy.requestFocus();
Helper.hideKeyboard(getActivity());
}
}

View File

@ -9,23 +9,6 @@
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<EditText
android:id="@+id/etDummy"
android:layout_width="0sp"
android:layout_height="0sp" />
<com.m2049r.xmrwallet.layout.DropDownEditText
android:id="@+id/etDaemonAddress"
style="@style/MoneroEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/prompt_daemon"
android:imeOptions="actionDone"
android:inputType="textWebEmailAddress|textNoSuggestions"
android:maxLines="1"
android:textIsSelectable="true" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"

View File

@ -4,6 +4,23 @@
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etDummy"
android:layout_width="0sp"
android:layout_height="0sp" />
<com.m2049r.xmrwallet.layout.DropDownEditText
android:id="@+id/etDaemonAddress"
style="@style/MoneroEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/prompt_daemon"
android:imeOptions="actionDone"
android:inputType="textWebEmailAddress|textNoSuggestions"
android:maxLines="1"
android:textIsSelectable="true" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">