From 7268b07c6c7ddada410582ea01ca14ed8ca0943d Mon Sep 17 00:00:00 2001 From: pokkst Date: Tue, 4 Oct 2022 03:08:52 -0500 Subject: [PATCH] Use executorservice --- .../java/net/mynero/wallet/MoneroApplication.java | 9 +++++++++ .../fragment/dialog/SendBottomSheetDialog.java | 8 +++++--- .../fragment/onboarding/OnboardingFragment.java | 9 ++++++--- .../fragment/settings/SettingsFragment.java | 15 +++++++++------ .../fragment/settings/SettingsViewModel.java | 9 ++++++--- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/MoneroApplication.java b/app/src/main/java/net/mynero/wallet/MoneroApplication.java index ead129c..4686c7c 100644 --- a/app/src/main/java/net/mynero/wallet/MoneroApplication.java +++ b/app/src/main/java/net/mynero/wallet/MoneroApplication.java @@ -5,11 +5,20 @@ import android.app.Application; import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.NightmodeHelper; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + public class MoneroApplication extends Application { + private ExecutorService executor = null; @Override public void onCreate() { super.onCreate(); new PrefService(this); NightmodeHelper.getAndSetPreferredNightmode(); + executor = Executors.newFixedThreadPool(16); + } + + public ExecutorService getExecutor() { + return executor; } } diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java index 8139962..ccaa130 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java @@ -2,7 +2,6 @@ package net.mynero.wallet.fragment.dialog; import android.app.Activity; import android.content.Context; -import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -26,6 +25,7 @@ import com.google.zxing.client.android.Intents; import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanOptions; +import net.mynero.wallet.MoneroApplication; import net.mynero.wallet.R; import net.mynero.wallet.model.CoinsInfo; import net.mynero.wallet.model.PendingTransaction; @@ -38,6 +38,8 @@ import net.mynero.wallet.util.UriData; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class SendBottomSheetDialog extends BottomSheetDialogFragment { private final MutableLiveData _sendingMax = new MutableLiveData<>(false); @@ -234,7 +236,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment { } private void sendTx(PendingTransaction pendingTx) { - AsyncTask.execute(() -> { + ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { boolean success = TxService.getInstance().sendTx(pendingTx); Activity activity = getActivity(); if (activity != null) { @@ -252,7 +254,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment { } private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) { - AsyncTask.execute(() -> { + ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { try { PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos); if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) { diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java index 57ee921..c70f736 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java @@ -1,6 +1,5 @@ package net.mynero.wallet.fragment.onboarding; -import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -19,6 +18,7 @@ import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import net.mynero.wallet.MainActivity; +import net.mynero.wallet.MoneroApplication; import net.mynero.wallet.R; import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.WalletManager; @@ -26,6 +26,8 @@ import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; import java.io.File; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class OnboardingFragment extends Fragment { @@ -52,7 +54,7 @@ public class OnboardingFragment extends Fragment { moreOptionsChevronImageView.setOnClickListener(view12 -> mViewModel.onMoreOptionsClicked()); createWalletButton.setOnClickListener(view1 -> { - AsyncTask.execute(() -> { + ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { createOrImportWallet( walletPasswordEditText.getText().toString(), walletSeedEditText.getText().toString().trim(), @@ -115,6 +117,7 @@ public class OnboardingFragment extends Fragment { wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, "", restoreHeight); } Wallet.Status walletStatus = wallet.getStatus(); + wallet.close(); boolean ok = walletStatus.isOk(); 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.runOnUiThread(mainActivity::onBackPressed); } 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()); } } } diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java index 1865ba7..21a46e4 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java @@ -1,6 +1,5 @@ package net.mynero.wallet.fragment.settings; -import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -23,6 +22,7 @@ import androidx.fragment.app.FragmentManager; import androidx.lifecycle.ViewModelProvider; import androidx.navigation.fragment.NavHostFragment; +import net.mynero.wallet.MoneroApplication; import net.mynero.wallet.R; import net.mynero.wallet.data.DefaultNodes; 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.NightmodeHelper; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + public class SettingsFragment extends Fragment implements PasswordBottomSheetDialog.PasswordListener, NodeSelectionBottomSheetDialog.NodeSelectionDialogListener, AddNodeBottomSheetDialog.AddNodeListener { private SettingsViewModel mViewModel; @@ -54,7 +57,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia public void afterTextChanged(Editable editable) { if (mViewModel != null) { 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) { if (mViewModel != null) { 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); } - mViewModel.updateProxy(); + mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); }); displaySeedButton.setOnClickListener(view1 -> { @@ -221,8 +224,8 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia public void onNodeSelected() { Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress())); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); - mViewModel.updateProxy(); - AsyncTask.execute(() -> { + mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); + ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { WalletManager.getInstance().getWallet().init(0); WalletManager.getInstance().getWallet().startRefresh(); }); diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java index 0410940..be143a6 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java @@ -1,22 +1,25 @@ package net.mynero.wallet.fragment.settings; -import android.os.AsyncTask; import android.util.Patterns; import androidx.lifecycle.ViewModel; +import net.mynero.wallet.MoneroApplication; import net.mynero.wallet.data.DefaultNodes; import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + public class SettingsViewModel extends ViewModel { private String proxyAddress = ""; private String proxyPort = ""; - public void updateProxy() { - AsyncTask.execute(() -> { + public void updateProxy(MoneroApplication application) { + application.getExecutor().execute(() -> { boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); 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");