Use executorservice

This commit is contained in:
pokkst 2022-10-04 03:08:52 -05:00
parent 66770f9f7c
commit 7268b07c6c
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
5 changed files with 35 additions and 15 deletions

View File

@ -5,11 +5,20 @@ import android.app.Application;
import net.mynero.wallet.service.PrefService; import net.mynero.wallet.service.PrefService;
import net.mynero.wallet.util.NightmodeHelper; import net.mynero.wallet.util.NightmodeHelper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MoneroApplication extends Application { public class MoneroApplication extends Application {
private ExecutorService executor = null;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
new PrefService(this); new PrefService(this);
NightmodeHelper.getAndSetPreferredNightmode(); NightmodeHelper.getAndSetPreferredNightmode();
executor = Executors.newFixedThreadPool(16);
}
public ExecutorService getExecutor() {
return executor;
} }
} }

View File

@ -2,7 +2,6 @@ package net.mynero.wallet.fragment.dialog;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -26,6 +25,7 @@ import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions; import com.journeyapps.barcodescanner.ScanOptions;
import net.mynero.wallet.MoneroApplication;
import net.mynero.wallet.R; import net.mynero.wallet.R;
import net.mynero.wallet.model.CoinsInfo; import net.mynero.wallet.model.CoinsInfo;
import net.mynero.wallet.model.PendingTransaction; import net.mynero.wallet.model.PendingTransaction;
@ -38,6 +38,8 @@ import net.mynero.wallet.util.UriData;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SendBottomSheetDialog extends BottomSheetDialogFragment { public class SendBottomSheetDialog extends BottomSheetDialogFragment {
private final MutableLiveData<Boolean> _sendingMax = new MutableLiveData<>(false); private final MutableLiveData<Boolean> _sendingMax = new MutableLiveData<>(false);
@ -234,7 +236,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
} }
private void sendTx(PendingTransaction pendingTx) { private void sendTx(PendingTransaction pendingTx) {
AsyncTask.execute(() -> { ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> {
boolean success = TxService.getInstance().sendTx(pendingTx); boolean success = TxService.getInstance().sendTx(pendingTx);
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) { if (activity != null) {
@ -252,7 +254,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
} }
private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) { private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) {
AsyncTask.execute(() -> { ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> {
try { try {
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos); PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos);
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) { if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {

View File

@ -1,6 +1,5 @@
package net.mynero.wallet.fragment.onboarding; package net.mynero.wallet.fragment.onboarding;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -19,6 +18,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import net.mynero.wallet.MainActivity; import net.mynero.wallet.MainActivity;
import net.mynero.wallet.MoneroApplication;
import net.mynero.wallet.R; import net.mynero.wallet.R;
import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.Wallet;
import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.model.WalletManager;
@ -26,6 +26,8 @@ import net.mynero.wallet.service.PrefService;
import net.mynero.wallet.util.Constants; import net.mynero.wallet.util.Constants;
import java.io.File; import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OnboardingFragment extends Fragment { public class OnboardingFragment extends Fragment {
@ -52,7 +54,7 @@ public class OnboardingFragment extends Fragment {
moreOptionsChevronImageView.setOnClickListener(view12 -> mViewModel.onMoreOptionsClicked()); moreOptionsChevronImageView.setOnClickListener(view12 -> mViewModel.onMoreOptionsClicked());
createWalletButton.setOnClickListener(view1 -> { createWalletButton.setOnClickListener(view1 -> {
AsyncTask.execute(() -> { ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> {
createOrImportWallet( createOrImportWallet(
walletPasswordEditText.getText().toString(), walletPasswordEditText.getText().toString(),
walletSeedEditText.getText().toString().trim(), walletSeedEditText.getText().toString().trim(),
@ -115,6 +117,7 @@ public class OnboardingFragment extends Fragment {
wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, "", restoreHeight); wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, "", restoreHeight);
} }
Wallet.Status walletStatus = wallet.getStatus(); Wallet.Status walletStatus = wallet.getStatus();
wallet.close();
boolean ok = walletStatus.isOk(); boolean ok = walletStatus.isOk();
walletFile.delete(); // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too. walletFile.delete(); // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too.
@ -122,7 +125,7 @@ public class OnboardingFragment extends Fragment {
mainActivity.init(walletFile, walletPassword); mainActivity.init(walletFile, walletPassword);
mainActivity.runOnUiThread(mainActivity::onBackPressed); mainActivity.runOnUiThread(mainActivity::onBackPressed);
} else { } else {
Toast.makeText(mainActivity, getString(R.string.create_wallet_failed, walletStatus.getErrorString()), Toast.LENGTH_SHORT).show(); mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, getString(R.string.create_wallet_failed, walletStatus.getErrorString()), Toast.LENGTH_SHORT).show());
} }
} }
} }

View File

@ -1,6 +1,5 @@
package net.mynero.wallet.fragment.settings; package net.mynero.wallet.fragment.settings;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -23,6 +22,7 @@ import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
import net.mynero.wallet.MoneroApplication;
import net.mynero.wallet.R; import net.mynero.wallet.R;
import net.mynero.wallet.data.DefaultNodes; import net.mynero.wallet.data.DefaultNodes;
import net.mynero.wallet.data.Node; import net.mynero.wallet.data.Node;
@ -38,6 +38,9 @@ import net.mynero.wallet.util.Constants;
import net.mynero.wallet.util.DayNightMode; import net.mynero.wallet.util.DayNightMode;
import net.mynero.wallet.util.NightmodeHelper; import net.mynero.wallet.util.NightmodeHelper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SettingsFragment extends Fragment implements PasswordBottomSheetDialog.PasswordListener, NodeSelectionBottomSheetDialog.NodeSelectionDialogListener, AddNodeBottomSheetDialog.AddNodeListener { public class SettingsFragment extends Fragment implements PasswordBottomSheetDialog.PasswordListener, NodeSelectionBottomSheetDialog.NodeSelectionDialogListener, AddNodeBottomSheetDialog.AddNodeListener {
private SettingsViewModel mViewModel; private SettingsViewModel mViewModel;
@ -54,7 +57,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
if (mViewModel != null) { if (mViewModel != null) {
mViewModel.setProxyAddress(editable.toString()); mViewModel.setProxyAddress(editable.toString());
mViewModel.updateProxy(); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication()));
} }
} }
}; };
@ -71,7 +74,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
if (mViewModel != null) { if (mViewModel != null) {
mViewModel.setProxyPort(editable.toString()); mViewModel.setProxyPort(editable.toString());
mViewModel.updateProxy(); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication()));
} }
} }
}; };
@ -142,7 +145,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
proxySettingsLayout.setVisibility(View.GONE); proxySettingsLayout.setVisibility(View.GONE);
} }
mViewModel.updateProxy(); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication()));
}); });
displaySeedButton.setOnClickListener(view1 -> { displaySeedButton.setOnClickListener(view1 -> {
@ -221,8 +224,8 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
public void onNodeSelected() { public void onNodeSelected() {
Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress())); Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress()));
selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress()));
mViewModel.updateProxy(); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication()));
AsyncTask.execute(() -> { ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> {
WalletManager.getInstance().getWallet().init(0); WalletManager.getInstance().getWallet().init(0);
WalletManager.getInstance().getWallet().startRefresh(); WalletManager.getInstance().getWallet().startRefresh();
}); });

View File

@ -1,22 +1,25 @@
package net.mynero.wallet.fragment.settings; package net.mynero.wallet.fragment.settings;
import android.os.AsyncTask;
import android.util.Patterns; import android.util.Patterns;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import net.mynero.wallet.MoneroApplication;
import net.mynero.wallet.data.DefaultNodes; import net.mynero.wallet.data.DefaultNodes;
import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.model.WalletManager;
import net.mynero.wallet.service.PrefService; import net.mynero.wallet.service.PrefService;
import net.mynero.wallet.util.Constants; import net.mynero.wallet.util.Constants;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SettingsViewModel extends ViewModel { public class SettingsViewModel extends ViewModel {
private String proxyAddress = ""; private String proxyAddress = "";
private String proxyPort = ""; private String proxyPort = "";
public void updateProxy() { public void updateProxy(MoneroApplication application) {
AsyncTask.execute(() -> { application.getExecutor().execute(() -> {
boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false);
String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress()); String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress());
boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1");