diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java index bd7d9219..3a65cffd 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java @@ -18,6 +18,7 @@ package com.m2049r.xmrwallet; import android.app.Activity; import android.app.AlertDialog; +import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -33,9 +34,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ListView; @@ -54,110 +52,28 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -public class LoginActivity extends Activity { +public class LoginActivity extends Activity implements LoginFragment.LoginFragmentListener { static final String TAG = "LoginActivity"; - static final int MIN_DAEMON_VERSION = 65544; static final int DAEMON_TIMEOUT = 500; // deamon must respond in 500ms - ListView listView; - List walletList = new ArrayList<>(); - List displayedList = new ArrayList<>(); - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_login); - - final EditText etDaemonAddress = (EditText) findViewById(R.id.etDaemonAddress); - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - etDaemonAddress.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(etDaemonAddress, InputMethodManager.SHOW_IMPLICIT); - } - }); - 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)) { - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); - return false; - } - return false; - } - }); - - ToggleButton tbMainNet = (ToggleButton) findViewById(R.id.tbMainNet); - tbMainNet.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - boolean mainnet = ((ToggleButton) v).isChecked(); // current state - savePrefs(true); // use previous state as we just clicked it - if (mainnet) { - setDaemon(daemonMainNet); - } else { - setDaemon(daemonTestNet); - } - filterList(); - ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); - } - }); - - loadPrefs(); - - filterList(); - - listView = (ListView) findViewById(R.id.list); - ArrayAdapter adapter = new ArrayAdapter<>(this, - android.R.layout.simple_list_item_1, android.R.id.text1, this.displayedList); - - listView.setAdapter(adapter); - - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - EditText tvDaemonAddress = (EditText) findViewById(R.id.etDaemonAddress); - if (tvDaemonAddress.getText().toString().length() == 0) { - Toast.makeText(LoginActivity.this, getString(R.string.prompt_daemon_missing), Toast.LENGTH_LONG).show(); - return; - } - - String itemValue = (String) listView.getItemAtPosition(position); - if ((isMainNet() && itemValue.charAt(1) != '4') - || (!isMainNet() && itemValue.charAt(1) != '9')) { - Toast.makeText(LoginActivity.this, getString(R.string.prompt_wrong_net), Toast.LENGTH_LONG).show(); - return; - } - - final int preambleLength = "[123456] ".length(); - if (itemValue.length() <= (preambleLength)) { - Toast.makeText(LoginActivity.this, getString(R.string.panic), Toast.LENGTH_LONG).show(); - return; - } - setWalletDaemon(); - if (!checkWalletDaemon()) { - Toast.makeText(LoginActivity.this, getString(R.string.warn_daemon_unavailable), Toast.LENGTH_LONG).show(); - return; - } - - // looking good - savePrefs(false); - - String wallet = itemValue.substring(preambleLength); - promptPassword(wallet); - } - }); + setContentView(R.layout.login_activity); + if (savedInstanceState != null) { + return; + } if (Helper.getWritePermission(this)) { - new LoadListTask().execute(); + startLoginFragment(); } else { Log.i(TAG, "Waiting for permissions"); } } // adapted from http://www.mkyong.com/android/android-prompt-user-input-dialog-example/ - void promptPassword(final String wallet) { + @Override + public void promptPassword(final String wallet) { Context context = LoginActivity.this; LayoutInflater li = LayoutInflater.from(context); View promptsView = li.inflate(R.layout.prompt_password, null); @@ -209,6 +125,13 @@ public class LoginActivity extends Activity { alertDialog.show(); } + private boolean checkWalletPassword(String walletName, String password) { + String walletPath = new File(Helper.getStorageRoot(getApplicationContext()), + walletName + ".keys").getAbsolutePath(); + // only test view key + return WalletManager.getInstance().verifyWalletPassword(walletPath, password, true); + } + private void processPasswordEntry(String walletName, String pass) { if (checkWalletPassword(walletName, pass)) { startWallet(walletName, pass); @@ -217,43 +140,25 @@ public class LoginActivity extends Activity { } } - private boolean checkWalletDaemon() { -// if (android.os.Build.VERSION.SDK_INT > 9) { - StrictMode.ThreadPolicy prevPolicy = StrictMode.getThreadPolicy(); - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder(prevPolicy).permitNetwork().build(); - StrictMode.setThreadPolicy(policy); - String d[] = WalletManager.getInstance().getDaemonAddress().split(":"); - String host = d[0]; - int port = Integer.parseInt(d[1]); - Socket socket = new Socket(); - long a = new Date().getTime(); - try { - socket.connect(new InetSocketAddress(host, port), DAEMON_TIMEOUT); - socket.close(); - } catch (IOException ex) { - Log.d(TAG, "Cannot reach daemon " + host + ":" + port + " because " + ex.getLocalizedMessage()); - return false; - } finally { - StrictMode.setThreadPolicy(prevPolicy); - } - long b = new Date().getTime(); - Log.d(TAG, "Daemon is " + (b - a) + "ms away."); - int version = WalletManager.getInstance().getDaemonVersion(); - Log.d(TAG, "Daemon is v" + version); - return (version >= MIN_DAEMON_VERSION); + //////////////////////////////////////// + // LoginFragment.LoginFragmentListener + //////////////////////////////////////// + @Override + public SharedPreferences getPrefs() { + return getPreferences(Context.MODE_PRIVATE); } - private boolean checkWalletPassword(String walletName, String password) { - String walletPath = new File(Helper.getStorageRoot(getApplicationContext()), - walletName + ".keys").getAbsolutePath(); - // only test view key - return WalletManager.getInstance().verifyWalletPassword(walletPath, password, true); + @Override + public File getStorageRoot() { + return Helper.getStorageRoot(getApplicationContext()); } + //////////////////////////////////////// + //////////////////////////////////////// + @Override protected void onPause() { Log.d(TAG, "onPause()"); - savePrefs(false); super.onPause(); } @@ -263,76 +168,6 @@ public class LoginActivity extends Activity { Log.d(TAG, "onResume()"); } - boolean isMainNet() { - ToggleButton tbMainNet = (ToggleButton) findViewById(R.id.tbMainNet); - return tbMainNet.isChecked(); - } - - void setMainNet(boolean mainnet) { - ToggleButton tbMainNet = (ToggleButton) findViewById(R.id.tbMainNet); - tbMainNet.setChecked(mainnet); - } - - String getDaemon() { - EditText tvDaemonAddress = (EditText) findViewById(R.id.etDaemonAddress); - return tvDaemonAddress.getText().toString(); - } - - void setDaemon(String address) { - EditText tvDaemonAddress = (EditText) findViewById(R.id.etDaemonAddress); - tvDaemonAddress.setText(address); - } - - private static final String PREF_DAEMON_TESTNET = "daemon_testnet"; - private static final String PREF_DAEMON_MAINNET = "daemon_mainnet"; - private static final String PREF_MAINNET = "mainnet"; - - private String daemonTestNet; - private String daemonMainNet; - - void loadPrefs() { - SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE); - - boolean mainnet = sharedPref.getBoolean(PREF_MAINNET, false); - daemonMainNet = sharedPref.getString(PREF_DAEMON_MAINNET, "localhost:18081"); - daemonTestNet = sharedPref.getString(PREF_DAEMON_TESTNET, "localhost:28081"); - - setMainNet(mainnet); - if (mainnet) { - setDaemon(daemonMainNet); - } else { - setDaemon(daemonTestNet); - } - } - - void savePrefs(boolean usePreviousState) { - // save the daemon address for the net - boolean mainnet = isMainNet() ^ usePreviousState; - String daemon = getDaemon(); - if (mainnet) { - daemonMainNet = daemon; - } else { - daemonTestNet = daemon; - } - - SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putBoolean(PREF_MAINNET, mainnet); - editor.putString(PREF_DAEMON_MAINNET, daemonMainNet); - editor.putString(PREF_DAEMON_TESTNET, daemonTestNet); - editor.apply(); - } - - private void setWalletDaemon() { - boolean testnet = !isMainNet(); - String daemon = getDaemon(); - - if (!daemon.contains(":")) { - daemon = daemon + (testnet ? ":28081" : ":18081"); - } - - WalletManager.getInstance().setDaemon(daemon, testnet); - } void startWallet(String walletName, String walletPassword) { Log.d(TAG, "startWallet()"); @@ -342,44 +177,6 @@ public class LoginActivity extends Activity { startActivity(intent); } - private void filterList() { - displayedList.clear(); - char x = isMainNet() ? '4' : '9'; - for (String s : walletList) { - if (s.charAt(1) == x) displayedList.add(s); - } - } - - private class LoadListTask extends AsyncTask { - protected void onPreExecute() { - //Toast.makeText(LoginActivity.this, getString(R.string.status_walletlist_loading), Toast.LENGTH_LONG).show(); - } - - protected Integer doInBackground(String... params) { - WalletManager mgr = WalletManager.getInstance(); - List walletInfos = - mgr.findWallets(Helper.getStorageRoot(getApplicationContext())); - - walletList.clear(); - for (WalletManager.WalletInfo walletInfo : walletInfos) { - Log.d(TAG, walletInfo.address); - String displayAddress = walletInfo.address; - if (displayAddress.length() == 95) { - displayAddress = walletInfo.address.substring(0, 6); - } - walletList.add("[" + displayAddress + "] " + walletInfo.name); - } - return 0; - } - - protected void onPostExecute(Integer result) { - if (result == 0) { - filterList(); - ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); - } - } - } - @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { Log.d(TAG, "onRequestPermissionsResult()"); @@ -388,7 +185,7 @@ public class LoginActivity extends Activity { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - new LoadListTask().execute(); + startLoginFragment(); } else { String msg = getString(R.string.message_strorage_not_permitted); Log.e(TAG, msg); @@ -398,5 +195,18 @@ public class LoginActivity extends Activity { default: } } -} + void startLoginFragment() { + // Create a new Fragment to be placed in the activity layout + Fragment fragment = new LoginFragment(); + + // In case this activity was started with special instructions from an + // Intent, pass the Intent's extras to the fragment as arguments + //firstFragment.setArguments(getIntent().getExtras()); + + // Add the fragment to the 'fragment_container' FrameLayout + getFragmentManager().beginTransaction() + .add(R.id.fragment_container, fragment).commit(); + Log.d(TAG, "fragment added"); + } +} diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java b/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java new file mode 100644 index 00000000..5dc08f1f --- /dev/null +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2017 m2049r + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.m2049r.xmrwallet; + +import android.app.Fragment; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.StrictMode; +import android.util.Log; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.WindowManager; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; +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 android.widget.ToggleButton; + +import com.m2049r.xmrwallet.model.WalletManager; +import com.m2049r.xmrwallet.util.Helper; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class LoginFragment extends Fragment { + private static final String TAG = "LoginFragment"; + + ListView listView; + List walletList = new ArrayList<>(); + List displayedList = new ArrayList<>(); + + ToggleButton tbMainNet; + EditText etDaemonAddress; + + LoginFragment.LoginFragmentListener activityCallback; + + // Container Activity must implement this interface + public interface LoginFragmentListener { + SharedPreferences getPrefs(); + + File getStorageRoot(); + + void promptPassword(final String wallet); + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof LoginFragment.LoginFragmentListener) { + this.activityCallback = (LoginFragment.LoginFragmentListener) context; + } else { + throw new ClassCastException(context.toString() + + " must implement WalletFragmentListener"); + } + } + + @Override + public void onPause() { + Log.d(TAG, "onPause()"); + savePrefs(false); + super.onPause(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.login_fragment, container, false); + + tbMainNet = (ToggleButton) view.findViewById(R.id.tbMainNet); + etDaemonAddress = (EditText) view.findViewById(R.id.etDaemonAddress); + + getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + + etDaemonAddress.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(etDaemonAddress, InputMethodManager.SHOW_IMPLICIT); + } + }); + 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)) { + getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); + return false; + } + return false; + } + }); + + tbMainNet.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean mainnet = ((ToggleButton) v).isChecked(); // current state + savePrefs(true); // use previous state as we just clicked it + if (mainnet) { + setDaemon(daemonMainNet); + } else { + setDaemon(daemonTestNet); + } + filterList(); + ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); + } + }); + + loadPrefs(); + + listView = (ListView) view.findViewById(R.id.list); + ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), + android.R.layout.simple_list_item_1, android.R.id.text1, this.displayedList); + + listView.setAdapter(adapter); + + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + EditText tvDaemonAddress = (EditText) getView().findViewById(R.id.etDaemonAddress); + if (tvDaemonAddress.getText().toString().length() == 0) { + Toast.makeText(getActivity(), getString(R.string.prompt_daemon_missing), Toast.LENGTH_LONG).show(); + return; + } + + String itemValue = (String) listView.getItemAtPosition(position); + if ((isMainNet() && itemValue.charAt(1) != '4') + || (!isMainNet() && itemValue.charAt(1) != '9')) { + Toast.makeText(getActivity(), getString(R.string.prompt_wrong_net), Toast.LENGTH_LONG).show(); + return; + } + + final int preambleLength = "[123456] ".length(); + if (itemValue.length() <= (preambleLength)) { + Toast.makeText(getActivity(), getString(R.string.panic), Toast.LENGTH_LONG).show(); + return; + } + if (!checkAndSetWalletDaemon(getDaemon(), !isMainNet())) { + Toast.makeText(getActivity(), getString(R.string.warn_daemon_unavailable), Toast.LENGTH_LONG).show(); + return; + } + + // looking good + savePrefs(false); + + String wallet = itemValue.substring(preambleLength); + activityCallback.promptPassword(wallet); + } + }); + loadList(); + return view; + } + + private void filterList() { + displayedList.clear(); + char x = isMainNet() ? '4' : '9'; + for (String s : walletList) { + if (s.charAt(1) == x) displayedList.add(s); + } + } + + private void loadList() { + WalletManager mgr = WalletManager.getInstance(); + List walletInfos = + mgr.findWallets(activityCallback.getStorageRoot()); + + walletList.clear(); + for (WalletManager.WalletInfo walletInfo : walletInfos) { + Log.d(TAG, walletInfo.address); + String displayAddress = walletInfo.address; + if (displayAddress.length() == 95) { + displayAddress = walletInfo.address.substring(0, 6); + } + walletList.add("[" + displayAddress + "] " + walletInfo.name); + } + filterList(); + ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); + + } + + + boolean isMainNet() { + return tbMainNet.isChecked(); + } + + void setMainNet(boolean mainnet) { + tbMainNet.setChecked(mainnet); + } + + String getDaemon() { + return etDaemonAddress.getText().toString(); + } + + void setDaemon(String address) { + etDaemonAddress.setText(address); + } + + private static final String PREF_DAEMON_TESTNET = "daemon_testnet"; + private static final String PREF_DAEMON_MAINNET = "daemon_mainnet"; + private static final String PREF_MAINNET = "mainnet"; + + private String daemonTestNet; + private String daemonMainNet; + + void loadPrefs() { + SharedPreferences sharedPref = activityCallback.getPrefs(); + + boolean mainnet = sharedPref.getBoolean(PREF_MAINNET, false); + daemonMainNet = sharedPref.getString(PREF_DAEMON_MAINNET, "localhost:18081"); + daemonTestNet = sharedPref.getString(PREF_DAEMON_TESTNET, "localhost:28081"); + + setMainNet(mainnet); + if (mainnet) { + setDaemon(daemonMainNet); + } else { + setDaemon(daemonTestNet); + } + } + + void savePrefs(boolean usePreviousState) { + // save the daemon address for the net + boolean mainnet = isMainNet() ^ usePreviousState; + String daemon = getDaemon(); + if (mainnet) { + daemonMainNet = daemon; + } else { + daemonTestNet = daemon; + } + + SharedPreferences sharedPref = activityCallback.getPrefs(); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putBoolean(PREF_MAINNET, mainnet); + editor.putString(PREF_DAEMON_MAINNET, daemonMainNet); + editor.putString(PREF_DAEMON_TESTNET, daemonTestNet); + editor.apply(); + } + + private boolean checkAndSetWalletDaemon(String daemonAddress, boolean testnet) { + String d[] = daemonAddress.split(":"); + if (d.length > 2) return false; + if (d.length < 1) return false; + String host = d[0]; + int port; + if (d.length == 2) { + try { + port = Integer.parseInt(d[1]); + } catch (NumberFormatException ex) { + return false; + } + } else { + port = (testnet ? 28081 : 18081); + } +// if (android.os.Build.VERSION.SDK_INT > 9) { + StrictMode.ThreadPolicy prevPolicy = StrictMode.getThreadPolicy(); + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder(prevPolicy).permitNetwork().build(); + StrictMode.setThreadPolicy(policy); + Socket socket = new Socket(); + long a = new Date().getTime(); + try { + socket.connect(new InetSocketAddress(host, port), LoginActivity.DAEMON_TIMEOUT); + socket.close(); + } catch (IOException ex) { + Log.d(TAG, "Cannot reach daemon " + host + ":" + port + " because " + ex.getLocalizedMessage()); + return false; + } finally { + StrictMode.setThreadPolicy(prevPolicy); + } + long b = new Date().getTime(); + Log.d(TAG, "Daemon is " + (b - a) + "ms away."); + + WalletManager mgr = WalletManager.getInstance(); + mgr.setDaemon(daemonAddress, testnet); + int version = mgr.getDaemonVersion(); + Log.d(TAG, "Daemon is v" + version); + return (version >= WalletActivity.MIN_DAEMON_VERSION); + } +} diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java index d0ec8bef..22bc86f7 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java @@ -35,6 +35,8 @@ public class WalletActivity extends Activity implements WalletFragment.WalletFra WalletService.Observer { private static final String TAG = "WalletActivity"; + static final int MIN_DAEMON_VERSION = 65544; + public static final String REQUEST_ID = "id"; public static final String REQUEST_PW = "pw"; @@ -283,7 +285,7 @@ public class WalletActivity extends Activity implements WalletFragment.WalletFra @Override public void onProgress(final String text) { - Log.d(TAG, "PROGRESS: " + text); + //Log.d(TAG, "PROGRESS: " + text); // TODO check which fragment is loaded final WalletFragment walletFragment = (WalletFragment) getFragmentManager().findFragmentById(R.id.fragment_container); diff --git a/app/src/main/res/layout/login_activity.xml b/app/src/main/res/layout/login_activity.xml new file mode 100644 index 00000000..017e4013 --- /dev/null +++ b/app/src/main/res/layout/login_activity.xml @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/login_fragment.xml similarity index 100% rename from app/src/main/res/layout/activity_login.xml rename to app/src/main/res/layout/login_fragment.xml