wallet rename + tweaks

This commit is contained in:
m2049r 2017-09-03 22:07:59 +02:00
parent 4b2d52fbe6
commit e662d5a9c0
6 changed files with 76 additions and 36 deletions

2
.idea/.gitignore vendored
View File

@ -1,2 +1,2 @@
workspace.xml
markdown-navigator*
markdown-*

View File

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

View File

@ -161,11 +161,51 @@ public class LoginActivity extends AppCompatActivity
}
}
private class AsyncRename extends AsyncTask<String, Void, Boolean> {
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();

View File

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

View File

@ -11,12 +11,14 @@
<string name="backup_progress">Backup in progress</string>
<string name="archive_progress">Archive in progress</string>
<string name="rename_progress">Rename in progress</string>
<string name="service_progress">Wrapping things up &#8230;</string>
<string name="service_progress">Wrapping things up &#8230;\nThis can take a while!</string>
<string name="backup_success">Backup successful</string>
<string name="backup_failed">Backup failed!</string>
<string name="archive_success">Archive successful</string>
<string name="archive_failed">Archive failed!</string>
<string name="delete_failed">Delete failed!</string>
<string name="rename_failed">Rename failed!</string>
@ -93,6 +95,7 @@
<string name="generate_wallet_watchonly">&lt;Watch Only Wallet&gt;</string>
<string name="generate_wallet_exists">Wallet exists! Choose another name</string>
<string name="generate_wallet_dot">Wallet name may not begin with \'.\'</string>
<string name="generate_wallet_created">Wallet created</string>
<string name="generate_wallet_create_failed">Wallet create failed</string>
<string name="generate_wallet_create_failed_1">Wallet create failed (1/2)</string>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="xmrwallet" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" version="4">
<module external.linked.project.id="xmrwallet" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>