From e662d5a9c0d394b54799be417f7ad157328934d7 Mon Sep 17 00:00:00 2001 From: m2049r <30435443+m2049r@users.noreply.github.com> Date: Sun, 3 Sep 2017 22:07:59 +0200 Subject: [PATCH] wallet rename + tweaks --- .idea/.gitignore | 2 +- .../m2049r/xmrwallet/GenerateFragment.java | 4 + .../com/m2049r/xmrwallet/LoginActivity.java | 92 +++++++++++++------ .../com/m2049r/xmrwallet/WalletActivity.java | 7 +- app/src/main/res/values/strings.xml | 5 +- xmrwallet.iml | 2 +- 6 files changed, 76 insertions(+), 36 deletions(-) diff --git a/.idea/.gitignore b/.idea/.gitignore index 6faf0832..45c1d3be 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,2 +1,2 @@ workspace.xml -markdown-navigator* +markdown-* diff --git a/app/src/main/java/com/m2049r/xmrwallet/GenerateFragment.java b/app/src/main/java/com/m2049r/xmrwallet/GenerateFragment.java index a5d87c3b..c3a7f07c 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/GenerateFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/GenerateFragment.java @@ -295,6 +295,10 @@ public class GenerateFragment extends Fragment { private void generateWallet() { String name = etWalletName.getText().toString(); if (name.length() == 0) return; + if (name.charAt(0)=='.') { + Toast.makeText(getActivity(), getString(R.string.generate_wallet_dot), Toast.LENGTH_LONG).show(); + etWalletName.requestFocus(); + } File walletFile = Helper.getWalletFile(getActivity(), name); if (WalletManager.getInstance().walletExists(walletFile)) { Toast.makeText(getActivity(), getString(R.string.generate_wallet_exists), Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java index e6cc5fe9..9c02f9cd 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java @@ -161,11 +161,51 @@ public class LoginActivity extends AppCompatActivity } } + private class AsyncRename extends AsyncTask { + ProgressDialog progressDialog = new MyProgressDialog(LoginActivity.this, R.string.rename_progress); + + @Override + protected void onPreExecute() { + super.onPreExecute(); + progressDialog.show(); + LoginActivity.this.asyncWaitTask = this; + } + + @Override + protected Boolean doInBackground(String... params) { + if (params.length != 2) return false; + File walletFile = Helper.getWalletFile(LoginActivity.this, params[0]); + String newName = params[1]; + return renameWallet(walletFile, newName); + } + + @Override + protected void onPostExecute(Boolean result) { + super.onPostExecute(result); + progressDialog.dismiss(); + if (result) { + reloadWalletList(); + } else { + Toast.makeText(LoginActivity.this, getString(R.string.rename_failed), Toast.LENGTH_LONG).show(); + } + LoginActivity.this.asyncWaitTask = null; + } + } + + // copy + delete seems safer than rename bevause we call rollback easily + boolean renameWallet(File walletFile, String newName) { + if (copyWallet(walletFile, new File(walletFile.getParentFile(), newName), false)) { + deleteWallet(walletFile); + return true; + } else { + return false; + } + } + @Override - public void onWalletRename(String walletName) { + public void onWalletRename(final String walletName) { Log.d(TAG, "rename for wallet ." + walletName + "."); if (checkServiceRunning()) return; - final File walletFile = Helper.getWalletFile(this, walletName); LayoutInflater li = LayoutInflater.from(this); View promptsView = li.inflate(R.layout.prompt_rename, null); @@ -175,7 +215,7 @@ public class LoginActivity extends AppCompatActivity final EditText etRename = (EditText) promptsView.findViewById(R.id.etRename); final TextView tvRenameLabel = (TextView) promptsView.findViewById(R.id.tvRenameLabel); - tvRenameLabel.setText(getString(R.string.prompt_rename) + " " + walletFile.getName()); + tvRenameLabel.setText(getString(R.string.prompt_rename) + " " + walletName); // set dialog message alertDialogBuilder @@ -185,10 +225,7 @@ public class LoginActivity extends AppCompatActivity public void onClick(DialogInterface dialog, int id) { Helper.hideKeyboardAlways(LoginActivity.this); String newName = etRename.getText().toString(); - if (!renameWallet(walletFile, newName)) { - Toast.makeText(LoginActivity.this, getString(R.string.rename_failed), Toast.LENGTH_LONG).show(); - } - reloadWalletList(); + new AsyncRename().execute(walletName, newName); } }) .setNegativeButton("Cancel", @@ -209,10 +246,7 @@ public class LoginActivity extends AppCompatActivity Helper.hideKeyboardAlways(LoginActivity.this); String newName = etRename.getText().toString(); dialog.cancel(); - if (!renameWallet(walletFile, newName)) { - Toast.makeText(LoginActivity.this, getString(R.string.rename_failed), Toast.LENGTH_LONG).show(); - } - reloadWalletList(); + new AsyncRename().execute(walletName, newName); return false; } return false; @@ -261,6 +295,9 @@ public class LoginActivity extends AppCompatActivity File walletFile = Helper.getWalletFile(LoginActivity.this, walletName); File backupFile = new File(backupFolder, walletName); Log.d(TAG, "backup " + walletFile.getAbsolutePath() + " to " + backupFile.getAbsolutePath()); + // TODO probably better to copy to a new file and then rename + // then if something fails we have the old backup at least + // or just create a new backup every time and keep n old backups return copyWallet(walletFile, backupFile, true); } @@ -298,7 +335,7 @@ public class LoginActivity extends AppCompatActivity if (result) { reloadWalletList(); } else { - Toast.makeText(LoginActivity.this, getString(R.string.backup_failed), Toast.LENGTH_LONG).show(); + Toast.makeText(LoginActivity.this, getString(R.string.archive_failed), Toast.LENGTH_LONG).show(); } LoginActivity.this.asyncWaitTask = null; } @@ -740,29 +777,24 @@ public class LoginActivity extends AppCompatActivity return status; } - // TODO use move - boolean renameWallet(File walletFile, String newName) { - if (copyWallet(walletFile, new File(walletFile.getParentFile(), newName), false)) { - deleteWallet(walletFile); - return true; + boolean walletExists(File walletFile, boolean any) { + File dir = walletFile.getParentFile(); + String name = walletFile.getName(); + if (any) { + return new File(dir, name).exists() + || new File(dir, name + ".keys").exists() + || new File(dir, name + ".address.txt").exists(); } else { - return false; + return new File(dir, name).exists() + && new File(dir, name + ".keys").exists() + && new File(dir, name + ".address.txt").exists(); } } - boolean walletExists(File walletFile) { - File dir = walletFile.getParentFile(); - String name = walletFile.getName(); - boolean exists = new File(dir, name).exists(); - exists = new File(dir, name + ".keys").exists() && exists; - exists = new File(dir, name + ".address.txt").exists() && exists; - return exists; - } - boolean copyWallet(File srcWallet, File dstWallet, boolean overwrite) { - Log.d(TAG, "src=" + srcWallet.exists() + " dst=" + dstWallet.exists()); - if (walletExists(dstWallet) && !overwrite) return false; - if (!walletExists(srcWallet)) return false; + //Log.d(TAG, "src=" + srcWallet.exists() + " dst=" + dstWallet.exists()); + if (walletExists(dstWallet, true) && !overwrite) return false; + if (!walletExists(srcWallet, false)) return false; boolean success = false; File srcDir = srcWallet.getParentFile(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java index fc9f340e..ea0e12a5 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java @@ -93,20 +93,21 @@ public class WalletActivity extends AppCompatActivity implements WalletFragment. } private void startWalletService() { - acquireWakeLock(); Bundle extras = getIntent().getExtras(); if (extras != null) { + acquireWakeLock(); String walletId = extras.getString(REQUEST_ID); String walletPassword = extras.getString(REQUEST_PW); connectWalletService(walletId, walletPassword); } else { - throw new IllegalStateException("No extras passed! Panic!"); + finish(); + //throw new IllegalStateException("No extras passed! Panic!"); } } private void stopWalletService() { - releaseWakeLock(); disconnectWalletService(); + releaseWakeLock(); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6be50c02..d93c00b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,12 +11,14 @@ Backup in progress Archive in progress + Rename in progress - Wrapping things up … + Wrapping things up …\nThis can take a while! Backup successful Backup failed! Archive successful + Archive failed! Delete failed! Rename failed! @@ -93,6 +95,7 @@ <Watch Only Wallet> Wallet exists! Choose another name + Wallet name may not begin with \'.\' Wallet created Wallet create failed Wallet create failed (1/2) diff --git a/xmrwallet.iml b/xmrwallet.iml index a36ea9c4..bb050e81 100644 --- a/xmrwallet.iml +++ b/xmrwallet.iml @@ -1,5 +1,5 @@ - +