From 451371cd9298ca31b896583d0fd4a921468d925a Mon Sep 17 00:00:00 2001 From: m2049r Date: Fri, 5 Apr 2024 15:57:37 +0200 Subject: [PATCH] refactor onBackPressed to use the callback dispatcher (#937) --- .../com/m2049r/xmrwallet/BaseActivity.java | 41 +++++++++++++---- .../xmrwallet/GenerateReviewFragment.java | 16 +++++-- .../com/m2049r/xmrwallet/LoginActivity.java | 44 +++++-------------- .../com/m2049r/xmrwallet/LoginFragment.java | 26 ++++++++--- .../com/m2049r/xmrwallet/NodeFragment.java | 12 +++++ .../xmrwallet/OnBackPressedListener.java | 21 --------- .../com/m2049r/xmrwallet/WalletActivity.java | 23 ++-------- .../xmrwallet/fragment/send/SendFragment.java | 27 +++++++----- .../ledger/LedgerProgressDialog.java | 5 --- 9 files changed, 104 insertions(+), 111 deletions(-) delete mode 100644 app/src/main/java/com/m2049r/xmrwallet/OnBackPressedListener.java diff --git a/app/src/main/java/com/m2049r/xmrwallet/BaseActivity.java b/app/src/main/java/com/m2049r/xmrwallet/BaseActivity.java index 4a4881bb..b714ea27 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/BaseActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/BaseActivity.java @@ -17,11 +17,14 @@ package com.m2049r.xmrwallet; import android.content.Context; +import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.PowerManager; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.CallSuper; +import androidx.fragment.app.FragmentActivity; import com.m2049r.xmrwallet.data.BarcodeData; import com.m2049r.xmrwallet.dialog.ProgressDialog; @@ -35,18 +38,13 @@ public class BaseActivity extends SecureActivity ProgressDialog progressDialog = null; - private class SimpleProgressDialog extends ProgressDialog { + private static class SimpleProgressDialog extends ProgressDialog { SimpleProgressDialog(Context context, int msgId) { super(context); setCancelable(false); setMessage(context.getString(msgId)); } - - @Override - public void onBackPressed() { - // prevent back button - } } @Override @@ -59,13 +57,15 @@ public class BaseActivity extends SecureActivity progressDialog = new SimpleProgressDialog(BaseActivity.this, msgId); if (delayMillis > 0) { Handler handler = new Handler(); - handler.postDelayed(new Runnable() { - public void run() { - if (progressDialog != null) progressDialog.show(); + handler.postDelayed(() -> { + if (progressDialog != null) { + progressDialog.show(); + disableBackPressed(); } }, delayMillis); } else { progressDialog.show(); + disableBackPressed(); } } @@ -75,6 +75,7 @@ public class BaseActivity extends SecureActivity progressDialog = new LedgerProgressDialog(BaseActivity.this, mode); Ledger.setListener((Ledger.Listener) progressDialog); progressDialog.show(); + disableBackPressed(); } @Override @@ -87,6 +88,28 @@ public class BaseActivity extends SecureActivity progressDialog.dismiss(); } progressDialog = null; + enableBackPressed(); + } + + OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + // no going back + } + }; + + public void disableBackPressed() { + backPressedCallback.setEnabled(true); + } + + public void enableBackPressed() { + backPressedCallback.setEnabled(false); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getOnBackPressedDispatcher().addCallback(this, backPressedCallback); } static final int RELEASE_WAKE_LOCK_DELAY = 5000; // millisconds diff --git a/app/src/main/java/com/m2049r/xmrwallet/GenerateReviewFragment.java b/app/src/main/java/com/m2049r/xmrwallet/GenerateReviewFragment.java index cb40b843..1fd57d9d 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/GenerateReviewFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/GenerateReviewFragment.java @@ -40,6 +40,7 @@ import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; @@ -98,6 +99,13 @@ public class GenerateReviewFragment extends Fragment { private String walletPath; private String walletName; + private OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + // nothing + } + }; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -160,6 +168,9 @@ public class GenerateReviewFragment extends Fragment { walletPath = args.getString(REQUEST_PATH); localPassword = args.getString(REQUEST_PASSWORD); showDetails(); + if (type.equals(GenerateReviewFragment.VIEW_TYPE_ACCEPT)) { + backPressedCallback.setEnabled(true); + } return view; } @@ -420,14 +431,11 @@ public class GenerateReviewFragment extends Fragment { pbProgress.setVisibility(View.INVISIBLE); } - boolean backOk() { - return !type.equals(GenerateReviewFragment.VIEW_TYPE_ACCEPT); - } - @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + requireActivity().getOnBackPressedDispatcher().addCallback(this, backPressedCallback); } @Override diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java index 6cdc60b2..c3b2f443 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java @@ -295,7 +295,7 @@ public class LoginActivity extends BaseActivity toolbar.setOnButtonListener(type -> { switch (type) { case Toolbar.BUTTON_BACK: - onBackPressed(); + getOnBackPressedDispatcher().onBackPressed(); break; case Toolbar.BUTTON_CLOSE: finish(); @@ -1086,30 +1086,6 @@ public class LoginActivity extends BaseActivity } } - @Override - public void onBackPressed() { - Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container); - if (f instanceof GenerateReviewFragment) { - if (((GenerateReviewFragment) f).backOk()) { - super.onBackPressed(); - } - } else if (f instanceof NodeFragment) { - if (!((NodeFragment) f).isRefreshing()) { - super.onBackPressed(); - } else { - Toast.makeText(LoginActivity.this, getString(R.string.node_refresh_wait), Toast.LENGTH_LONG).show(); - } - } else if (f instanceof LoginFragment) { - if (((LoginFragment) f).isFabOpen()) { - ((LoginFragment) f).animateFAB(); - } else { - super.onBackPressed(); - } - } else { - super.onBackPressed(); - } - } - @Override public boolean onOptionsItemSelected(MenuItem item) { final int id = item.getItemId(); @@ -1294,21 +1270,21 @@ public class LoginActivity extends BaseActivity if (!Ledger.check()) { Ledger.disconnect(); runOnUiThread(() -> Toast.makeText(LoginActivity.this, - getString(R.string.toast_ledger_start_app, usbDevice.getProductName()), - Toast.LENGTH_SHORT) + getString(R.string.toast_ledger_start_app, usbDevice.getProductName()), + Toast.LENGTH_SHORT) .show()); } else { registerDetachReceiver(); onLedgerAction(); runOnUiThread(() -> Toast.makeText(LoginActivity.this, - getString(R.string.toast_ledger_attached, usbDevice.getProductName()), - Toast.LENGTH_SHORT) + getString(R.string.toast_ledger_attached, usbDevice.getProductName()), + Toast.LENGTH_SHORT) .show()); } } catch (IOException ex) { runOnUiThread(() -> Toast.makeText(LoginActivity.this, - getString(R.string.open_wallet_ledger_missing), - Toast.LENGTH_SHORT) + getString(R.string.open_wallet_ledger_missing), + Toast.LENGTH_SHORT) .show()); } } @@ -1368,8 +1344,8 @@ public class LoginActivity extends BaseActivity Timber.i("Ledger detached!"); if (device != null) runOnUiThread(() -> Toast.makeText(LoginActivity.this, - getString(R.string.toast_ledger_detached, device.getProductName()), - Toast.LENGTH_SHORT) + getString(R.string.toast_ledger_detached, device.getProductName()), + Toast.LENGTH_SHORT) .show()); Ledger.disconnect(); onLedgerAction(); @@ -1383,7 +1359,7 @@ public class LoginActivity extends BaseActivity public void onLedgerAction() { Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (f instanceof GenerateFragment) { - onBackPressed(); + getOnBackPressedDispatcher().onBackPressed(); } else if (f instanceof LoginFragment) { if (((LoginFragment) f).isFabOpen()) { ((LoginFragment) f).animateFAB(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java b/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java index 5eff646f..8e920adb 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginFragment.java @@ -35,6 +35,7 @@ import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @@ -148,6 +149,13 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter //activityCallback.runOnNetCipher(this::pingSelectedNode); } + private OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + animateFAB(); + } + }; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -287,6 +295,7 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); } @Override @@ -295,7 +304,7 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter super.onCreateOptionsMenu(menu, inflater); } - private boolean isFabOpen = false; + private boolean fabOpen = false; private FloatingActionButton fab, fabNew, fabView, fabKey, fabSeed, fabImport, fabLedger; private RelativeLayout fabScreen; private RelativeLayout fabNewL, fabViewL, fabKeyL, fabSeedL, fabImportL, fabLedgerL; @@ -303,11 +312,16 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter private Animation fab_pulse; public boolean isFabOpen() { - return isFabOpen; + return fabOpen; + } + + private void setFabOpen(boolean value) { + fabOpen = value; + onBackPressedCallback.setEnabled(value); } public void animateFAB() { - if (isFabOpen) { // close the fab + if (isFabOpen()) { // close the fab fabScreen.setClickable(false); fabScreen.startAnimation(fab_close_screen); fab.startAnimation(rotate_backward); @@ -326,7 +340,7 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter fabImportL.startAnimation(fab_close); fabImport.setClickable(false); } - isFabOpen = false; + setFabOpen(false); } else { // open the fab fabScreen.setClickable(true); fabScreen.startAnimation(fab_open_screen); @@ -360,7 +374,7 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter fabImportL.startAnimation(fab_open); fabImport.setClickable(true); } - isFabOpen = true; + setFabOpen(true); } } @@ -372,7 +386,7 @@ public class LoginFragment extends Fragment implements WalletInfoAdapter.OnInter animateFAB(); } else if (id == R.id.fabNew) { fabScreen.setVisibility(View.INVISIBLE); - isFabOpen = false; + setFabOpen(false); activityCallback.onAddWallet(GenerateFragment.TYPE_NEW); } else if (id == R.id.fabView) { animateFAB(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/NodeFragment.java b/app/src/main/java/com/m2049r/xmrwallet/NodeFragment.java index 0bbc5a4d..70098d6f 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/NodeFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/NodeFragment.java @@ -31,6 +31,7 @@ import android.widget.Button; import android.widget.TextView; import android.widget.Toast; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; @@ -145,6 +146,13 @@ public class NodeFragment extends Fragment } } + private OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + Toast.makeText(requireActivity(), getString(R.string.node_refresh_wait), Toast.LENGTH_LONG).show(); + } + }; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -187,6 +195,7 @@ public class NodeFragment extends Fragment private boolean refresh(int type) { if (asyncFindNodes != null) return false; // ignore refresh request as one is ongoing + onBackPressedCallback.setEnabled(true); asyncFindNodes = new AsyncFindNodes(); updateRefreshElements(); asyncFindNodes.execute(type); @@ -197,6 +206,7 @@ public class NodeFragment extends Fragment public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); } @Override @@ -342,6 +352,7 @@ public class NodeFragment extends Fragment private void complete() { asyncFindNodes = null; + onBackPressedCallback.setEnabled(false); if (!isAdded()) return; //if (isCancelled()) return; tvPull.setText(getString(R.string.node_pull_hint)); @@ -575,6 +586,7 @@ public class NodeFragment extends Fragment } } } + } void restoreDefaultNodes() { diff --git a/app/src/main/java/com/m2049r/xmrwallet/OnBackPressedListener.java b/app/src/main/java/com/m2049r/xmrwallet/OnBackPressedListener.java deleted file mode 100644 index eb09125e..00000000 --- a/app/src/main/java/com/m2049r/xmrwallet/OnBackPressedListener.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2017 m2049r - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.m2049r.xmrwallet; - -public interface OnBackPressedListener { - boolean onBackPressed(); -} diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java index 7f2b3597..64757f5c 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java @@ -377,12 +377,12 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste switch (type) { case Toolbar.BUTTON_BACK: onDisposeRequest(); - onBackPressed(); + getOnBackPressedDispatcher().onBackPressed(); break; case Toolbar.BUTTON_CANCEL: onDisposeRequest(); Helper.hideKeyboard(WalletActivity.this); - WalletActivity.super.onBackPressed(); + getOnBackPressedDispatcher().onBackPressed(); break; case Toolbar.BUTTON_CLOSE: finish(); @@ -964,23 +964,6 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste return getWallet().getUnlockedBalance(); } - @Override - public void onBackPressed() { - if (drawer.isDrawerOpen(GravityCompat.START)) { - drawer.closeDrawer(GravityCompat.START); - return; - } - final Fragment fragment = getCurrentFragment(); - if (fragment instanceof OnBackPressedListener) { - if (!((OnBackPressedListener) fragment).onBackPressed()) { - super.onBackPressed(); - } - } else { - super.onBackPressed(); - } - Helper.hideKeyboard(this); - } - @Override public void onFragmentDone() { popFragmentStack(null); @@ -1201,7 +1184,7 @@ public class WalletActivity extends BaseActivity implements WalletFragment.Liste @Override public void onSubaddressSelected(@Nullable final Subaddress subaddress) { selectedSubaddressIndex = subaddress.getAddressIndex(); - onBackPressed(); + getOnBackPressedDispatcher().onBackPressed(); } @Override diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendFragment.java index ce82795b..877f84d6 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/send/SendFragment.java @@ -29,6 +29,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @@ -37,7 +38,6 @@ import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.ViewPager; import com.google.android.material.transition.MaterialContainerTransform; -import com.m2049r.xmrwallet.OnBackPressedListener; import com.m2049r.xmrwallet.OnUriScannedListener; import com.m2049r.xmrwallet.R; import com.m2049r.xmrwallet.WalletActivity; @@ -63,7 +63,7 @@ public class SendFragment extends Fragment SendAmountWizardFragment.Listener, SendConfirmWizardFragment.Listener, SendSuccessWizardFragment.Listener, - OnBackPressedListener, OnUriScannedListener { + OnUriScannedListener { final static public int MIXIN = 0; @@ -248,16 +248,18 @@ public class SendFragment extends Fragment private SpendViewPager spendViewPager; private SpendPagerAdapter pagerAdapter; - @Override - public boolean onBackPressed() { - if (isComitted()) return true; // no going back - if (spendViewPager.getCurrentItem() == 0) { - return false; - } else { - spendViewPager.previous(); - return true; + OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + if (isComitted()) return; // no going back + if (spendViewPager.getCurrentItem() == 0) { + setEnabled(false); + requireActivity().getOnBackPressedDispatcher().onBackPressed(); + } else { + spendViewPager.previous(); + } } - } + }; @Override public boolean onUriScanned(BarcodeData barcodeData) { @@ -546,8 +548,9 @@ public class SendFragment extends Fragment final MaterialContainerTransform transform = new MaterialContainerTransform(); transform.setDrawingViewId(R.id.fragment_container); transform.setDuration(getResources().getInteger(R.integer.tx_item_transition_duration)); - transform.setAllContainerColors(ThemeHelper.getThemedColor(getContext(), android.R.attr.colorBackground)); + transform.setAllContainerColors(ThemeHelper.getThemedColor(requireContext(), android.R.attr.colorBackground)); setSharedElementEnterTransition(transform); + requireActivity().getOnBackPressedDispatcher().addCallback(this, backPressedCallback); } @Override diff --git a/app/src/main/java/com/m2049r/xmrwallet/ledger/LedgerProgressDialog.java b/app/src/main/java/com/m2049r/xmrwallet/ledger/LedgerProgressDialog.java index 0c81cb2b..fc85a571 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/ledger/LedgerProgressDialog.java +++ b/app/src/main/java/com/m2049r/xmrwallet/ledger/LedgerProgressDialog.java @@ -46,11 +46,6 @@ public class LedgerProgressDialog extends ProgressDialog implements Ledger.Liste setMessage(context.getString(R.string.progress_ledger_progress)); } - @Override - public void onBackPressed() { - // prevent back button - } - private int firstSubaddress = Integer.MAX_VALUE; private boolean validate = false;