refactoring

This commit is contained in:
m2049r 2021-03-14 21:21:36 +01:00
parent 16870fcbb9
commit 12f135bb14
No known key found for this signature in database
GPG Key ID: 4386E69AF260078D
4 changed files with 180 additions and 273 deletions

View File

@ -16,6 +16,7 @@
package com.m2049r.xmrwallet; package com.m2049r.xmrwallet;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -43,6 +44,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.ShareActionProvider; import androidx.appcompat.widget.ShareActionProvider;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
@ -57,7 +59,6 @@ import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.m2049r.xmrwallet.BuildConfig;
import com.m2049r.xmrwallet.data.BarcodeData; import com.m2049r.xmrwallet.data.BarcodeData;
import com.m2049r.xmrwallet.data.Crypto; import com.m2049r.xmrwallet.data.Crypto;
import com.m2049r.xmrwallet.ledger.LedgerProgressDialog; import com.m2049r.xmrwallet.ledger.LedgerProgressDialog;
@ -74,6 +75,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import timber.log.Timber; import timber.log.Timber;
@ -86,7 +88,6 @@ public class ReceiveFragment extends Fragment {
private ExchangeView evAmount; private ExchangeView evAmount;
private TextView tvQrCode; private TextView tvQrCode;
private ImageView ivQrCode; private ImageView ivQrCode;
private View cvQrCode;
private ImageView ivQrCodeFull; private ImageView ivQrCodeFull;
private EditText etDummy; private EditText etDummy;
private ImageButton bCopyAddress; private ImageButton bCopyAddress;
@ -114,7 +115,6 @@ public class ReceiveFragment extends Fragment {
tvAddress = view.findViewById(R.id.tvAddress); tvAddress = view.findViewById(R.id.tvAddress);
etNotes = view.findViewById(R.id.etNotes); etNotes = view.findViewById(R.id.etNotes);
evAmount = view.findViewById(R.id.evAmount); evAmount = view.findViewById(R.id.evAmount);
cvQrCode = view.findViewById(R.id.cvQrCode);
ivQrCode = view.findViewById(R.id.qrCode); ivQrCode = view.findViewById(R.id.qrCode);
tvQrCode = view.findViewById(R.id.tvQrCode); tvQrCode = view.findViewById(R.id.tvQrCode);
ivQrCodeFull = view.findViewById(R.id.qrCodeFull); ivQrCodeFull = view.findViewById(R.id.qrCodeFull);
@ -128,40 +128,31 @@ public class ReceiveFragment extends Fragment {
enableCopyAddress(false); enableCopyAddress(false);
enableSubaddressButton(false); enableSubaddressButton(false);
evAmount.setOnNewAmountListener(new ExchangeView.OnNewAmountListener() { evAmount.setOnNewAmountListener(xmr -> {
@Override Timber.d("new amount = %s", xmr);
public void onNewAmount(String xmr) { generateQr();
Timber.d("new amount = %s", xmr);
generateQr();
}
}); });
evAmount.setOnFailedExchangeListener(new ExchangeView.OnFailedExchangeListener() { evAmount.setOnFailedExchangeListener(() -> {
@Override if (isAdded()) {
public void onFailedExchange() { clearQR();
if (isAdded()) { Toast.makeText(getActivity(), getString(R.string.message_exchange_failed), Toast.LENGTH_LONG).show();
clearQR();
Toast.makeText(getActivity(), getString(R.string.message_exchange_failed), Toast.LENGTH_LONG).show();
}
} }
}); });
final EditText notesEdit = etNotes.getEditText(); final EditText notesEdit = etNotes.getEditText();
notesEdit.setRawInputType(InputType.TYPE_CLASS_TEXT); notesEdit.setRawInputType(InputType.TYPE_CLASS_TEXT);
notesEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() { notesEdit.setOnEditorActionListener((v, actionId, event) -> {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN))
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
|| (actionId == EditorInfo.IME_ACTION_DONE)) { generateQr();
generateQr(); return true;
return true;
}
return false;
} }
return false;
}); });
notesEdit.addTextChangedListener(new TextWatcher() { notesEdit.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
@ -171,47 +162,33 @@ public class ReceiveFragment extends Fragment {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
} }
}); });
bSubaddress.setOnClickListener(new View.OnClickListener() { bSubaddress.setOnClickListener(v -> {
@Override enableSubaddressButton(false);
public void onClick(View v) { enableCopyAddress(false);
enableSubaddressButton(false);
enableCopyAddress(false);
final Runnable newAddress = new Runnable() { final Runnable newAddress = this::getNewSubaddress;
public void run() {
getNewSubaddress();
}
};
tvAddress.animate().alpha(0).setDuration(250) tvAddress.animate().alpha(0).setDuration(250)
.withEndAction(newAddress).start(); .withEndAction(newAddress).start();
});
view.findViewById(R.id.cvQrCode).setOnClickListener(v -> {
Helper.hideKeyboard(getActivity());
etDummy.requestFocus();
if (qrValid) {
ivQrCodeFull.setImageBitmap(((BitmapDrawable) ivQrCode.getDrawable()).getBitmap());
ivQrCodeFull.setVisibility(View.VISIBLE);
} else {
evAmount.doExchange();
} }
}); });
cvQrCode.setOnClickListener(new View.OnClickListener() { ivQrCodeFull.setOnClickListener(v -> {
@Override ivQrCodeFull.setImageBitmap(null);
public void onClick(View v) { ivQrCodeFull.setVisibility(View.GONE);
Helper.hideKeyboard(getActivity());
etDummy.requestFocus();
if (qrValid) {
ivQrCodeFull.setImageBitmap(((BitmapDrawable) ivQrCode.getDrawable()).getBitmap());
ivQrCodeFull.setVisibility(View.VISIBLE);
} else {
evAmount.doExchange();
}
}
});
ivQrCodeFull.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivQrCodeFull.setImageBitmap(null);
ivQrCodeFull.setVisibility(View.GONE);
}
}); });
showProgress(); showProgress();
@ -249,14 +226,14 @@ public class ReceiveFragment extends Fragment {
final MaterialContainerTransform transform = new MaterialContainerTransform(); final MaterialContainerTransform transform = new MaterialContainerTransform();
transform.setDrawingViewId(R.id.fragment_container); transform.setDrawingViewId(R.id.fragment_container);
transform.setDuration(getResources().getInteger(R.integer.tx_item_transition_duration)); transform.setDuration(getResources().getInteger(R.integer.tx_item_transition_duration));
transform.setAllContainerColors(ThemeHelper.getThemedColor(getContext(), R.attr.colorSurface)); transform.setAllContainerColors(ThemeHelper.getThemedColor(Objects.requireNonNull(getContext()), R.attr.colorSurface));
setSharedElementEnterTransition(transform); setSharedElementEnterTransition(transform);
} }
private ShareActionProvider shareActionProvider; private ShareActionProvider shareActionProvider;
@Override @Override
public void onCreateOptionsMenu(Menu menu, final MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.receive_menu, menu); inflater.inflate(R.menu.receive_menu, menu);
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
@ -327,7 +304,7 @@ public class ReceiveFragment extends Fragment {
} }
void copyAddress() { void copyAddress() {
Helper.clipBoardCopy(getActivity(), getString(R.string.label_copy_address), tvAddress.getText().toString()); Helper.clipBoardCopy(Objects.requireNonNull(getActivity()), getString(R.string.label_copy_address), tvAddress.getText().toString());
Toast.makeText(getActivity(), getString(R.string.message_copy_address), Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), getString(R.string.message_copy_address), Toast.LENGTH_SHORT).show();
} }
@ -388,6 +365,7 @@ public class ReceiveFragment extends Fragment {
GenerateReviewFragment.ProgressListener progressCallback = null; GenerateReviewFragment.ProgressListener progressCallback = null;
@SuppressLint("StaticFieldLeak")
private class AsyncShow extends AsyncTask<Void, Void, Boolean> { private class AsyncShow extends AsyncTask<Void, Void, Boolean> {
final private String walletPath; final private String walletPath;
final private String password; final private String password;
@ -441,8 +419,8 @@ public class ReceiveFragment extends Fragment {
new AsyncStore().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR); new AsyncStore().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR);
} }
@SuppressLint("StaticFieldLeak")
private class AsyncStore extends AsyncTask<String, Void, Boolean> { private class AsyncStore extends AsyncTask<String, Void, Boolean> {
@Override @Override
protected Boolean doInBackground(String... params) { protected Boolean doInBackground(String... params) {
if (params.length != 0) return false; if (params.length != 0) return false;
@ -527,12 +505,12 @@ public class ReceiveFragment extends Fragment {
canvas.save(); canvas.save();
// figure out how to scale the logo // figure out how to scale the logo
float scaleSize = 1.0f; float scaleSize = 1.0f;
while ((logoWidth / scaleSize) > (qrWidth / 5) || (logoHeight / scaleSize) > (qrHeight / 5)) { while ((logoWidth / scaleSize) > (qrWidth / 5.) || (logoHeight / scaleSize) > (qrHeight / 5.)) {
scaleSize *= 2; scaleSize *= 2;
} }
float sx = 1.0f / scaleSize; float sx = 1.0f / scaleSize;
canvas.scale(sx, sx, qrWidth / 2, qrHeight / 2); canvas.scale(sx, sx, qrWidth / 2f, qrHeight / 2f);
canvas.drawBitmap(logo, (qrWidth - logoWidth) / 2, (qrHeight - logoHeight) / 2, null); canvas.drawBitmap(logo, (qrWidth - logoWidth) / 2f, (qrHeight - logoHeight) / 2f, null);
canvas.restore(); canvas.restore();
return logoBitmap; return logoBitmap;
} }
@ -557,7 +535,7 @@ public class ReceiveFragment extends Fragment {
Listener listenerCallback = null; Listener listenerCallback = null;
@Override @Override
public void onAttach(Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
if (context instanceof Listener) { if (context instanceof Listener) {
this.listenerCallback = (Listener) context; this.listenerCallback = (Listener) context;
@ -592,6 +570,7 @@ public class ReceiveFragment extends Fragment {
new AsyncSubaddress().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR); new AsyncSubaddress().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR);
} }
@SuppressLint("StaticFieldLeak")
private class AsyncSubaddress extends AsyncTask<Void, Void, Boolean> { private class AsyncSubaddress extends AsyncTask<Void, Void, Boolean> {
private String newSubaddress; private String newSubaddress;
@ -626,11 +605,7 @@ public class ReceiveFragment extends Fragment {
wallet.getNumSubaddresses() - 1)); wallet.getNumSubaddresses() - 1));
generateQr(); generateQr();
enableCopyAddress(true); enableCopyAddress(true);
final Runnable resetSize = new Runnable() { final Runnable resetSize = () -> tvAddress.animate().setDuration(125).scaleX(1).scaleY(1).start();
public void run() {
tvAddress.animate().setDuration(125).scaleX(1).scaleY(1).start();
}
};
tvAddress.animate().alpha(1).setDuration(125) tvAddress.animate().alpha(1).setDuration(125)
.scaleX(1.2f).scaleY(1.2f) .scaleX(1.2f).scaleY(1.2f)
.withEndAction(resetSize).start(); .withEndAction(resetSize).start();

View File

@ -16,6 +16,7 @@
package com.m2049r.xmrwallet; package com.m2049r.xmrwallet;
import android.annotation.SuppressLint;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -205,7 +206,6 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
connectWalletService(walletId, password); connectWalletService(walletId, password);
} else { } else {
finish(); finish();
//throw new IllegalStateException("No extras passed! Panic!");
} }
} }
@ -270,47 +270,36 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { final int itemId = item.getItemId();
case R.id.action_rescan: if (itemId == R.id.action_rescan) {
onWalletRescan(); onWalletRescan();
return true; } else if (itemId == R.id.action_info) {
case R.id.action_info: onWalletDetails();
onWalletDetails(); } else if (itemId == R.id.action_credits) {
return true; CreditsFragment.display(getSupportFragmentManager());
case R.id.action_credits: } else if (itemId == R.id.action_share) {
CreditsFragment.display(getSupportFragmentManager()); onShareTxInfo();
return true; } else if (itemId == R.id.action_help_tx_info) {
case R.id.action_share: HelpFragment.display(getSupportFragmentManager(), R.string.help_tx_details);
onShareTxInfo(); } else if (itemId == R.id.action_help_wallet) {
return true; HelpFragment.display(getSupportFragmentManager(), R.string.help_wallet);
case R.id.action_help_tx_info: } else if (itemId == R.id.action_details_help) {
HelpFragment.display(getSupportFragmentManager(), R.string.help_tx_details); HelpFragment.display(getSupportFragmentManager(), R.string.help_details);
return true; } else if (itemId == R.id.action_details_changepw) {
case R.id.action_help_wallet: onWalletChangePassword();
HelpFragment.display(getSupportFragmentManager(), R.string.help_wallet); } else if (itemId == R.id.action_help_send) {
return true; HelpFragment.display(getSupportFragmentManager(), R.string.help_send);
case R.id.action_details_help: } else if (itemId == R.id.action_rename) {
HelpFragment.display(getSupportFragmentManager(), R.string.help_details); onAccountRename();
return true; } else if (itemId == R.id.action_streetmode) {
case R.id.action_details_changepw: if (isStreetMode()) { // disable streetmode
onWalletChangePassword(); onDisableStreetMode();
return true; } else {
case R.id.action_help_send: onEnableStreetMode();
HelpFragment.display(getSupportFragmentManager(), R.string.help_send); }
return true; } else
case R.id.action_rename: return super.onOptionsItemSelected(item);
onAccountRename(); return true;
return true;
case R.id.action_streetmode:
if (isStreetMode()) { // disable streetmode
onDisableStreetMode();
} else {
onEnableStreetMode();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
} }
private void updateStreetMode() { private void updateStreetMode() {
@ -326,12 +315,9 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
Helper.promptPassword(WalletActivity.this, getWallet().getName(), false, new Helper.PasswordAction() { Helper.promptPassword(WalletActivity.this, getWallet().getName(), false, new Helper.PasswordAction() {
@Override @Override
public void act(String walletName, String password, boolean fingerprintUsed) { public void act(String walletName, String password, boolean fingerprintUsed) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override enableStreetMode(false);
public void run() { updateStreetMode();
enableStreetMode(false);
updateStreetMode();
}
}); });
} }
@ -422,10 +408,8 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
case NetworkType_Mainnet: case NetworkType_Mainnet:
toolbar.setBackgroundResource(R.drawable.backgound_toolbar_mainnet); toolbar.setBackgroundResource(R.drawable.backgound_toolbar_mainnet);
break; break;
case NetworkType_Testnet:
toolbar.setBackgroundResource(ThemeHelper.getThemedResourceId(this, R.attr.colorPrimaryDark));
break;
case NetworkType_Stagenet: case NetworkType_Stagenet:
case NetworkType_Testnet:
toolbar.setBackgroundResource(ThemeHelper.getThemedResourceId(this, R.attr.colorPrimaryDark)); toolbar.setBackgroundResource(ThemeHelper.getThemedResourceId(this, R.attr.colorPrimaryDark));
break; break;
default: default:
@ -442,7 +426,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
private WalletService mBoundService = null; private WalletService mBoundService = null;
private boolean mIsBound = false; private boolean mIsBound = false;
private ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been // This is called when the connection with the service has been
// established, giving us the service object we can use to // established, giving us the service object we can use to
@ -590,18 +574,10 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
onProgress(-1); onProgress(-1);
saveWallet(); // save on first sync saveWallet(); // save on first sync
synced = true; synced = true;
runOnUiThread(new Runnable() { runOnUiThread(walletFragment::onSynced);
public void run() {
walletFragment.onSynced();
}
});
} }
} }
runOnUiThread(new Runnable() { runOnUiThread(() -> walletFragment.onRefreshed(wallet, full));
public void run() {
walletFragment.onRefreshed(wallet, full);
}
});
return true; return true;
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
// not in wallet fragment (probably send monero) // not in wallet fragment (probably send monero)
@ -613,13 +589,11 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
@Override @Override
public void onWalletStored(final boolean success) { public void onWalletStored(final boolean success) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
public void run() { if (success) {
if (success) { Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_unloaded), Toast.LENGTH_SHORT).show();
Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_unloaded), Toast.LENGTH_SHORT).show(); } else {
} else { Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_unload_failed), Toast.LENGTH_LONG).show();
Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_unload_failed), Toast.LENGTH_LONG).show();
}
} }
}); });
} }
@ -628,30 +602,23 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
@Override @Override
public void onWalletOpen(final Wallet.Device device) { public void onWalletOpen(final Wallet.Device device) {
switch (device) { if (device == Wallet.Device.Device_Ledger) {
case Device_Ledger: runOnUiThread(() -> showLedgerProgressDialog(LedgerProgressDialog.TYPE_RESTORE));
runOnUiThread(new Runnable() {
public void run() {
showLedgerProgressDialog(LedgerProgressDialog.TYPE_RESTORE);
}
});
} }
} }
@Override @Override
public void onWalletStarted(final Wallet.Status walletStatus) { public void onWalletStarted(final Wallet.Status walletStatus) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
public void run() { dismissProgressDialog();
dismissProgressDialog(); if (walletStatus == null) {
if (walletStatus == null) { // guess what went wrong
// guess what went wrong Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_connect_failed), Toast.LENGTH_LONG).show();
Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_connect_failed), Toast.LENGTH_LONG).show(); } else {
} else { if (Wallet.ConnectionStatus.ConnectionStatus_WrongVersion == walletStatus.getConnectionStatus())
if (Wallet.ConnectionStatus.ConnectionStatus_WrongVersion == walletStatus.getConnectionStatus()) Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_connect_wrongversion), Toast.LENGTH_LONG).show();
Toast.makeText(WalletActivity.this, getString(R.string.status_wallet_connect_wrongversion), Toast.LENGTH_LONG).show(); else if (!walletStatus.isOk())
else if (!walletStatus.isOk()) Toast.makeText(WalletActivity.this, walletStatus.getErrorString(), Toast.LENGTH_LONG).show();
Toast.makeText(WalletActivity.this, walletStatus.getErrorString(), Toast.LENGTH_LONG).show();
}
} }
}); });
if ((walletStatus == null) || (Wallet.ConnectionStatus.ConnectionStatus_Connected != walletStatus.getConnectionStatus())) { if ((walletStatus == null) || (Wallet.ConnectionStatus.ConnectionStatus_Connected != walletStatus.getConnectionStatus())) {
@ -664,12 +631,10 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
final WalletFragment walletFragment = (WalletFragment) final WalletFragment walletFragment = (WalletFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container); getSupportFragmentManager().findFragmentById(R.id.fragment_container);
runOnUiThread(new Runnable() { runOnUiThread(() -> {
public void run() { updateAccountsHeader();
updateAccountsHeader(); if (walletFragment != null) {
if (walletFragment != null) { walletFragment.onLoaded();
walletFragment.onLoaded();
}
} }
}); });
} }
@ -680,17 +645,15 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
try { try {
final SendFragment sendFragment = (SendFragment) final SendFragment sendFragment = (SendFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container); getSupportFragmentManager().findFragmentById(R.id.fragment_container);
runOnUiThread(new Runnable() { runOnUiThread(() -> {
public void run() { dismissProgressDialog();
dismissProgressDialog(); PendingTransaction.Status status = pendingTransaction.getStatus();
PendingTransaction.Status status = pendingTransaction.getStatus(); if (status != PendingTransaction.Status.Status_Ok) {
if (status != PendingTransaction.Status.Status_Ok) { String errorText = pendingTransaction.getErrorString();
String errorText = pendingTransaction.getErrorString(); getWallet().disposePendingTransaction();
getWallet().disposePendingTransaction(); sendFragment.onCreateTransactionFailed(errorText);
sendFragment.onCreateTransactionFailed(errorText); } else {
} else { sendFragment.onTransactionCreated(txTag, pendingTransaction);
sendFragment.onTransactionCreated(txTag, pendingTransaction);
}
} }
}); });
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
@ -706,11 +669,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
try { try {
final SendFragment sendFragment = (SendFragment) final SendFragment sendFragment = (SendFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container); getSupportFragmentManager().findFragmentById(R.id.fragment_container);
runOnUiThread(new Runnable() { runOnUiThread(() -> sendFragment.onSendTransactionFailed(error));
public void run() {
sendFragment.onSendTransactionFailed(error);
}
});
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
// not in spend fragment // not in spend fragment
Timber.d(ex.getLocalizedMessage()); Timber.d(ex.getLocalizedMessage());
@ -722,11 +681,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
try { try {
final SendFragment sendFragment = (SendFragment) final SendFragment sendFragment = (SendFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_container); getSupportFragmentManager().findFragmentById(R.id.fragment_container);
runOnUiThread(new Runnable() { runOnUiThread(() -> sendFragment.onTransactionSent(txId));
public void run() {
sendFragment.onTransactionSent(txId);
}
});
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
// not in spend fragment // not in spend fragment
Timber.d(ex.getLocalizedMessage()); Timber.d(ex.getLocalizedMessage());
@ -752,18 +707,16 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
@Override @Override
public void onProgress(final int n) { public void onProgress(final int n) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
public void run() { try {
try { WalletFragment walletFragment = (WalletFragment)
WalletFragment walletFragment = (WalletFragment) getSupportFragmentManager().findFragmentByTag(WalletFragment.class.getName());
getSupportFragmentManager().findFragmentByTag(WalletFragment.class.getName()); if (walletFragment != null)
if (walletFragment != null) walletFragment.setProgress(n);
walletFragment.setProgress(n); } catch (ClassCastException ex) {
} catch (ClassCastException ex) { // not in wallet fragment (probably send monero)
// not in wallet fragment (probably send monero) Timber.d(ex.getLocalizedMessage());
Timber.d(ex.getLocalizedMessage()); // keep calm and carry on
// keep calm and carry on
}
} }
}); });
} }
@ -973,22 +926,17 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
@NonNull int[] grantResults) {
Timber.d("onRequestPermissionsResult()"); Timber.d("onRequestPermissionsResult()");
switch (requestCode) { if (requestCode == Helper.PERMISSIONS_REQUEST_CAMERA) { // If request is cancelled, the result arrays are empty.
case Helper.PERMISSIONS_REQUEST_CAMERA: if (grantResults.length > 0
// If request is cancelled, the result arrays are empty. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (grantResults.length > 0 startScanFragment = true;
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else {
startScanFragment = true; String msg = getString(R.string.message_camera_not_permitted);
} else { Timber.e(msg);
String msg = getString(R.string.message_camera_not_permitted); Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
Timber.e(msg); }
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
break;
default:
} }
} }
@ -1034,7 +982,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
return getPreferences(Context.MODE_PRIVATE); return getPreferences(Context.MODE_PRIVATE);
} }
private List<Integer> accountIds = new ArrayList<>(); private final List<Integer> accountIds = new ArrayList<>();
// generate and cache unique ids for use in accounts list // generate and cache unique ids for use in accounts list
private int getAccountId(int accountIndex) { private int getAccountId(int accountIndex) {
@ -1115,39 +1063,33 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
alertDialogBuilder alertDialogBuilder
.setCancelable(false) .setCancelable(false)
.setPositiveButton(getString(R.string.label_ok), .setPositiveButton(getString(R.string.label_ok),
new DialogInterface.OnClickListener() { (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { Helper.hideKeyboardAlways(WalletActivity.this);
Helper.hideKeyboardAlways(WalletActivity.this); String newName = etRename.getText().toString();
String newName = etRename.getText().toString(); wallet.setAccountLabel(newName);
wallet.setAccountLabel(newName); updateAccountName();
updateAccountName();
}
}) })
.setNegativeButton(getString(R.string.label_cancel), .setNegativeButton(getString(R.string.label_cancel),
new DialogInterface.OnClickListener() { (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) { Helper.hideKeyboardAlways(WalletActivity.this);
Helper.hideKeyboardAlways(WalletActivity.this); dialog.cancel();
dialog.cancel();
}
}); });
final AlertDialog dialog = alertDialogBuilder.create(); final AlertDialog dialog = alertDialogBuilder.create();
Helper.showKeyboard(dialog); Helper.showKeyboard(dialog);
// accept keyboard "ok" // accept keyboard "ok"
etRename.setOnEditorActionListener(new TextView.OnEditorActionListener() { etRename.setOnEditorActionListener((v, actionId, event) -> {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN))
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
|| (actionId == EditorInfo.IME_ACTION_DONE)) { Helper.hideKeyboardAlways(WalletActivity.this);
Helper.hideKeyboardAlways(WalletActivity.this); String newName = etRename.getText().toString();
String newName = etRename.getText().toString(); dialog.cancel();
dialog.cancel(); wallet.setAccountLabel(newName);
wallet.setAccountLabel(newName); updateAccountName();
updateAccountName();
return false;
}
return false; return false;
} }
return false;
}); });
dialog.show(); dialog.show();
@ -1156,19 +1098,17 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
@Override @Override
public boolean onNavigationItemSelected(MenuItem item) { public boolean onNavigationItemSelected(MenuItem item) {
final int id = item.getItemId(); final int id = item.getItemId();
switch (id) { if (id == R.id.account_new) {
case R.id.account_new: addAccount();
addAccount(); } else {
break; Timber.d("NavigationDrawer ID=%d", id);
default: int accountIdx = accountIds.indexOf(id);
Timber.d("NavigationDrawer ID=%d", id); if (accountIdx >= 0) {
int accountIdx = accountIds.indexOf(id); Timber.d("found @%d", accountIdx);
if (accountIdx >= 0) { getWallet().setAccountIndex(accountIdx);
Timber.d("found @%d", accountIdx); }
getWallet().setAccountIndex(accountIdx); forceUpdate();
} drawer.closeDrawer(GravityCompat.START);
forceUpdate();
drawer.closeDrawer(GravityCompat.START);
} }
return true; return true;
} }
@ -1177,6 +1117,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste
new AsyncAddAccount().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR); new AsyncAddAccount().executeOnExecutor(MoneroThreadPoolExecutor.MONERO_THREAD_POOL_EXECUTOR);
} }
@SuppressLint("StaticFieldLeak")
private class AsyncAddAccount extends AsyncTask<Void, Void, Boolean> { private class AsyncAddAccount extends AsyncTask<Void, Void, Boolean> {
boolean dialogOpened = false; boolean dialogOpened = false;

View File

@ -58,13 +58,14 @@ import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import timber.log.Timber; import timber.log.Timber;
public class WalletFragment extends Fragment public class WalletFragment extends Fragment
implements TransactionInfoAdapter.OnInteractionListener { implements TransactionInfoAdapter.OnInteractionListener {
private TransactionInfoAdapter adapter; private TransactionInfoAdapter adapter;
private NumberFormat formatter = NumberFormat.getInstance(); private final NumberFormat formatter = NumberFormat.getInstance();
private TextView tvStreetView; private TextView tvStreetView;
private LinearLayout llBalance; private LinearLayout llBalance;
@ -81,7 +82,7 @@ public class WalletFragment extends Fragment
private Spinner sCurrency; private Spinner sCurrency;
private List<String> dismissedTransactions = new ArrayList<>(); private final List<String> dismissedTransactions = new ArrayList<>();
public void resetDismissedTransactions() { public void resetDismissedTransactions() {
dismissedTransactions.clear(); dismissedTransactions.clear();
@ -94,7 +95,7 @@ public class WalletFragment extends Fragment
} }
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
if (activityCallback.hasWallet()) if (activityCallback.hasWallet())
inflater.inflate(R.menu.wallet_menu, menu); inflater.inflate(R.menu.wallet_menu, menu);
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
@ -132,7 +133,7 @@ public class WalletFragment extends Fragment
List<String> currencies = new ArrayList<>(); List<String> currencies = new ArrayList<>();
currencies.add(Helper.BASE_CRYPTO); currencies.add(Helper.BASE_CRYPTO);
currencies.addAll(Arrays.asList(getResources().getStringArray(R.array.currency))); currencies.addAll(Arrays.asList(getResources().getStringArray(R.array.currency)));
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getContext(), R.layout.item_spinner_balance, currencies); ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(Objects.requireNonNull(getContext()), R.layout.item_spinner_balance, currencies);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sCurrency.setAdapter(spinnerAdapter); sCurrency.setAdapter(spinnerAdapter);
@ -268,24 +269,14 @@ public class WalletFragment extends Fragment
@Override @Override
public void onSuccess(final ExchangeRate exchangeRate) { public void onSuccess(final ExchangeRate exchangeRate) {
if (isAdded()) if (isAdded())
new Handler(Looper.getMainLooper()).post(new Runnable() { new Handler(Looper.getMainLooper()).post(() -> exchange(exchangeRate));
@Override
public void run() {
exchange(exchangeRate);
}
});
} }
@Override @Override
public void onError(final Exception e) { public void onError(final Exception e) {
Timber.e(e.getLocalizedMessage()); Timber.e(e.getLocalizedMessage());
if (isAdded()) if (isAdded())
new Handler(Looper.getMainLooper()).post(new Runnable() { new Handler(Looper.getMainLooper()).post(() -> exchangeFailed());
@Override
public void run() {
exchangeFailed();
}
});
} }
}); });
} else { } else {
@ -426,7 +417,6 @@ public class WalletFragment extends Fragment
void setActivityTitle(Wallet wallet) { void setActivityTitle(Wallet wallet) {
if (wallet == null) return; if (wallet == null) return;
walletTitle = wallet.getName(); walletTitle = wallet.getName();
String watchOnly = (wallet.isWatchOnly() ? getString(R.string.label_watchonly) : "");
walletSubtitle = wallet.getAccountLabel(); walletSubtitle = wallet.getAccountLabel();
activityCallback.setTitle(walletTitle, walletSubtitle); activityCallback.setTitle(walletTitle, walletSubtitle);
Timber.d("wallet title is %s", walletTitle); Timber.d("wallet title is %s", walletTitle);
@ -450,7 +440,7 @@ public class WalletFragment extends Fragment
balance = wallet.getBalance(); balance = wallet.getBalance();
unlockedBalance = wallet.getUnlockedBalance(); unlockedBalance = wallet.getUnlockedBalance();
refreshBalance(); refreshBalance();
String sync = ""; String sync;
if (!activityCallback.hasBoundService()) if (!activityCallback.hasBoundService())
throw new IllegalStateException("WalletService not bound."); throw new IllegalStateException("WalletService not bound.");
Wallet.ConnectionStatus daemonConnected = activityCallback.getConnectionStatus(); Wallet.ConnectionStatus daemonConnected = activityCallback.getConnectionStatus();
@ -519,7 +509,7 @@ public class WalletFragment extends Fragment
} }
@Override @Override
public void onAttach(Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
if (context instanceof Listener) { if (context instanceof Listener) {
this.activityCallback = (Listener) context; this.activityCallback = (Listener) context;
@ -562,7 +552,7 @@ public class WalletFragment extends Fragment
//TODO figure out why gunther disappears on return from send although he is still set //TODO figure out why gunther disappears on return from send although he is still set
if (enable) { if (enable) {
if (streetGunther == null) if (streetGunther == null)
streetGunther = ContextCompat.getDrawable(getContext(), R.drawable.ic_gunther_streetmode); streetGunther = ContextCompat.getDrawable(Objects.requireNonNull(getContext()), R.drawable.ic_gunther_streetmode);
ivStreetGunther.setImageDrawable(streetGunther); ivStreetGunther.setImageDrawable(streetGunther);
} else } else
ivStreetGunther.setImageDrawable(null); ivStreetGunther.setImageDrawable(null);

View File

@ -55,6 +55,7 @@ import com.m2049r.xmrwallet.widget.DotBar;
import com.m2049r.xmrwallet.widget.Toolbar; import com.m2049r.xmrwallet.widget.Toolbar;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Objects;
import timber.log.Timber; import timber.log.Timber;
@ -311,7 +312,7 @@ public class SendFragment extends Fragment
SparseArray<WeakReference<SendWizardFragment>> myFragments = new SparseArray<>(); SparseArray<WeakReference<SendWizardFragment>> myFragments = new SparseArray<>();
public SpendPagerAdapter(FragmentManager fm) { public SpendPagerAdapter(FragmentManager fm) {
super(fm); super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
} }
public void addSuccess() { public void addSuccess() {
@ -546,12 +547,12 @@ public class SendFragment extends Fragment
final MaterialContainerTransform transform = new MaterialContainerTransform(); final MaterialContainerTransform transform = new MaterialContainerTransform();
transform.setDrawingViewId(R.id.fragment_container); transform.setDrawingViewId(R.id.fragment_container);
transform.setDuration(getResources().getInteger(R.integer.tx_item_transition_duration)); transform.setDuration(getResources().getInteger(R.integer.tx_item_transition_duration));
transform.setAllContainerColors(ThemeHelper.getThemedColor(getContext(), R.attr.colorSurface)); transform.setAllContainerColors(ThemeHelper.getThemedColor(Objects.requireNonNull(getContext()), R.attr.colorSurface));
setSharedElementEnterTransition(transform); setSharedElementEnterTransition(transform);
} }
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.send_menu, menu); inflater.inflate(R.menu.send_menu, menu);
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }