From 602a57cbbef7cdb08eb94da1dc26f1bec98683ad Mon Sep 17 00:00:00 2001 From: pokkst Date: Thu, 8 Sep 2022 02:24:34 -0500 Subject: [PATCH] fix onboarding bugs. tor no longer works, so i'm investigating. --- .../com/m2049r/xmrwallet/MainActivity.java | 1 - .../xmrwallet/fragment/home/HomeFragment.java | 22 +++++++----- .../onboarding/OnboardingFragment.java | 35 ++++++++++++++++--- .../xmrwallet/service/BlockchainService.java | 5 --- .../service/MoneroHandlerThread.java | 6 ++-- .../main/res/layout/fragment_onboarding.xml | 16 +++++++-- 6 files changed, 61 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java b/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java index 9799fcf..45f9e4a 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java @@ -71,7 +71,6 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre public void init(File walletFile, String password) { Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password); - WalletManager.getInstance().setProxy("127.0.0.1:9050"); thread = new MoneroHandlerThread("WalletService", wallet, this); this.txService = new TxService(this, thread); this.balanceService = new BalanceService(this, thread); diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java index f1f6072..9647325 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java @@ -45,6 +45,7 @@ import java.util.Collections; public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxInfoAdapterListener { private HomeViewModel mViewModel; + long startHeight = 0; @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @@ -114,15 +115,20 @@ public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxI if(blockchainService != null) { blockchainService.height.observe(getViewLifecycleOwner(), height -> { Wallet wallet = WalletManager.getInstance().getWallet(); - long daemonHeight = wallet.getDaemonBlockChainHeight(); - int syncPct = (int)blockchainService.getSyncPercentage(); - progressBar.setIndeterminate(height <= 1 || daemonHeight <= 0); - if(height > 1 && daemonHeight > 1) { - progressBar.setProgress(syncPct); - - if(wallet.isSynchronized()) { - progressBar.setVisibility(View.INVISIBLE); + if (!wallet.isSynchronized()) { + if (startHeight == 0 && height != 1) { + startHeight = height; } + long daemonHeight = blockchainService.getDaemonHeight(); + long n = daemonHeight - height; + int x = 100 - Math.round(100f * n / (1f * daemonHeight - startHeight)); + progressBar.setIndeterminate(height <= 1 || daemonHeight <= 0); + if (height > 1 && daemonHeight > 1) { + if (x == 0) x = 101; // indeterminate + progressBar.setProgress(x); + } + } else { + progressBar.setVisibility(View.INVISIBLE); } }); } diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java index 0153d53..d965e7d 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java @@ -13,7 +13,10 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; import androidx.lifecycle.ViewModelProvider; +import androidx.navigation.fragment.NavHostFragment; import com.m2049r.xmrwallet.MainActivity; import com.m2049r.xmrwallet.R; @@ -40,6 +43,7 @@ public class OnboardingFragment extends Fragment { mViewModel = new ViewModelProvider(this).get(OnboardingViewModel.class); EditText walletPasswordEditText = view.findViewById(R.id.wallet_password_edittext); EditText walletSeedEditText = view.findViewById(R.id.wallet_seed_edittext); + EditText walletRestoreHeightEditText = view.findViewById(R.id.wallet_restore_height_edittext); Button createWalletButton = view.findViewById(R.id.create_wallet_button); createWalletButton.setOnClickListener(view1 -> { String walletPassword = walletPasswordEditText.getText().toString(); @@ -47,20 +51,29 @@ public class OnboardingFragment extends Fragment { PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_PASSWORD, true).apply(); } String walletSeed = walletSeedEditText.getText().toString().trim(); + String restoreHeightText = walletRestoreHeightEditText.getText().toString().trim(); + long restoreHeight = -1; File walletFile = new File(getActivity().getApplicationInfo().dataDir, Constants.WALLET_NAME); Wallet wallet = null; if(walletSeed.isEmpty()) { - wallet = WalletManager.getInstance().createWallet(walletFile, walletPassword, Constants.MNEMONIC_LANGUAGE, 1); + wallet = WalletManager.getInstance().createWallet(walletFile, walletPassword, Constants.MNEMONIC_LANGUAGE, restoreHeight); } else { if(!checkMnemonic(walletSeed)) { Toast.makeText(getContext(), getString(R.string.invalid_mnemonic_code), Toast.LENGTH_SHORT).show(); return; } - wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, "", 0); + if(!restoreHeightText.isEmpty()) { + restoreHeight = Long.parseLong(restoreHeightText); + } + wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, "", restoreHeight); + } + boolean ok = wallet.getStatus().isOk(); + walletFile.delete(); // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too. + + if(ok) { + ((MainActivity)getActivity()).init(walletFile, walletPassword); + getActivity().onBackPressed(); } - wallet.close(); - ((MainActivity)getActivity()).init(walletFile, walletPassword); - getActivity().onBackPressed(); }); walletSeedEditText.addTextChangedListener(new TextWatcher() { @Override @@ -83,4 +96,16 @@ public class OnboardingFragment extends Fragment { private boolean checkMnemonic(String seed) { return (seed.split("\\s").length == 25); } + + private void navigate(int destination) { + FragmentActivity activity = getActivity(); + if (activity != null) { + FragmentManager fm = activity.getSupportFragmentManager(); + NavHostFragment navHostFragment = + (NavHostFragment) fm.findFragmentById(R.id.nav_host_fragment); + if (navHostFragment != null) { + navHostFragment.getNavController().navigate(destination); + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java b/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java index 487e07a..d8782e7 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java @@ -32,9 +32,4 @@ public class BlockchainService extends ServiceBase { public long getDaemonHeight() { return WalletManager.getInstance().getWallet().getDaemonBlockChainHeight(); } - - public double getSyncPercentage() { - double percentRaw = (double)getCurrentHeight()/(double)getDaemonHeight(); - return percentRaw * 100d; - } } diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/MoneroHandlerThread.java b/app/src/main/java/com/m2049r/xmrwallet/service/MoneroHandlerThread.java index 2d4e31f..8b8624a 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/MoneroHandlerThread.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/MoneroHandlerThread.java @@ -53,7 +53,9 @@ public class MoneroHandlerThread extends Thread implements WalletListener { @Override public void run() { + WalletManager.getInstance().setProxy("127.0.0.1:9050"); WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.XMRTW.getUri())); + wallet.setProxy("127.0.0.1:9050"); wallet.init(0); wallet.setListener(this); wallet.startRefresh(); @@ -73,9 +75,7 @@ public class MoneroHandlerThread extends Thread implements WalletListener { @Override public void newBlock(long height) { - if (height % 1000 == 0) { - refresh(); - } + refresh(); } @Override diff --git a/app/src/main/res/layout/fragment_onboarding.xml b/app/src/main/res/layout/fragment_onboarding.xml index dec3e0f..6dee492 100644 --- a/app/src/main/res/layout/fragment_onboarding.xml +++ b/app/src/main/res/layout/fragment_onboarding.xml @@ -26,8 +26,20 @@ android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginEnd="12dp" + android:layout_marginBottom="32dp" android:hint="Recovery phrase (optional)" - android:inputType="textPassword" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintBottom_toTopOf="@id/wallet_restore_height_edittext" + tools:visibility="visible"/> + \ No newline at end of file