diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java index 8d3b49dd3..320a91de8 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java @@ -48,6 +48,7 @@ import android.widget.SearchView; import android.widget.Toast; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import androidx.appcompat.app.ActionBarDrawerToggle; @@ -77,6 +78,7 @@ import com.nextcloud.android.sso.exceptions.TokenMismatchException; import com.nextcloud.android.sso.helper.SingleAccountHelper; import com.nextcloud.android.sso.ui.UiExceptionManager; +import com.sothree.slidinguppanel.SlidingUpPanelLayout; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -138,7 +140,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements public static final String ITEM_ID = "ITEM_ID"; public static final String TITLE = "TITLE"; - public static HashSet stayUnreadItems = new HashSet<>(); + public static HashSet stayUnreadItems = new HashSet<>(); private MenuItem menuItemOnlyUnread; private MenuItem menuItemDownloadMoreItems; @@ -148,6 +150,8 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements @VisibleForTesting(otherwise = PROTECTED) public ActivityNewsreaderBinding binding; + private boolean mBackOpensDrawer = false; + //private ServiceConnection mConnection = null; private ActionBarDrawerToggle drawerToggle; @@ -196,6 +200,40 @@ private boolean isUserLoggedIn() { return (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) != null); } + SlidingUpPanelLayout.PanelSlideListener panelSlideListener = new SlidingUpPanelLayout.PanelSlideListener() { + @Override + public void onPanelSlide(View panel, float slideOffset) { + } + + @Override + public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) { + boolean panelIsOpen = newState.equals(SlidingUpPanelLayout.PanelState.EXPANDED); + // in case the podcast panel is open, we need to close it first (intercept back presses) + onBackPressedCallback.setEnabled(panelIsOpen || mBackOpensDrawer); + } + }; + + OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + // we need to handle two cases: + // - The user has the "Open Sidebar on Backpress" option enabled + // - the callback need to be set because we want to close the podcast pane on back navigation (in case it's open) + // - set callback will be enabled/disabled based on whether the podcast pane is open/closed + // - The user has the "Open Sidebar on Backpress" option disabled + // - the callback needs to check first if the podcast is open - if so - close it and on + // the next back navigation open the sidebar - and then close the app + // - once the podcast pane is open - the callback will be disabled + // - the event listener (onDrawerClosed) will enable the back pressed callback again + @Override + public void handleOnBackPressed() { + Log.d(TAG, "handleOnBackPressed() 1"); + if (!handlePodcastBackPressed()) { + Log.d(TAG, "handleOnBackPressed() 2"); + binding.drawerLayout.openDrawer(GravityCompat.START); + setEnabled(false); + } + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { ((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this); @@ -243,6 +281,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); + onBackPressedCallback.setEnabled(mBackOpensDrawer); syncState(); } @@ -252,6 +291,9 @@ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); reloadCountNumbersOfSlidingPaneAdapter(); + // -> handleOnBackPressed() will disable it + // onBackPressedCallback.setEnabled(false); + syncState(); } }; @@ -266,10 +308,13 @@ public void onDrawerOpened(View drawerView) { drawerToggle.syncState(); } + getPodcastSlidingUpPanelLayout().addPanelSlideListener(panelSlideListener); + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); + //AppRater.app_launched(this); //AppRater.rateNow(this); - if (savedInstanceState == null) { //When the app starts (no orientation change) + if (savedInstanceState == null) { // When the app starts (no orientation change) updateDetailFragment(SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS.getValue(), true, null, true); } @@ -304,13 +349,13 @@ private void saveInstanceState(Bundle outState) { if (mSearchView != null) { mSearchString = mSearchView.getQuery().toString(); outState.putString(SEARCH_KEY, mSearchString); - } - } + } + } - private void restoreInstanceState(Bundle savedInstanceState) { - if (savedInstanceState.containsKey(ID_FEED_STRING) && - savedInstanceState.containsKey(IS_FOLDER_BOOLEAN) && - savedInstanceState.containsKey(OPTIONAL_FOLDER_ID)) { + private void restoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(ID_FEED_STRING) && + savedInstanceState.containsKey(IS_FOLDER_BOOLEAN) && + savedInstanceState.containsKey(OPTIONAL_FOLDER_ID)) { NewsListRecyclerAdapter adapter = new NewsListRecyclerAdapter(this, getNewsReaderDetailFragment().binding.list, this, mPostDelayHandler, mPrefs); @@ -537,12 +582,16 @@ private boolean syncFinishedHandler() { @Override protected void onResume() { + mBackOpensDrawer = mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER, false); + onBackPressedCallback.setEnabled(mBackOpensDrawer); + NewsReaderListFragment newsReaderListFragment = getSlidingListFragment(); if (newsReaderListFragment != null) { - newsReaderListFragment.reloadAdapter(); + newsReaderListFragment.reloadAdapter(); newsReaderListFragment.bindUserInfoToUI(); } - invalidateOptionsMenu(); + + invalidateOptionsMenu(); super.onResume(); } @@ -859,13 +908,6 @@ public boolean onMenuItemActionCollapse(MenuItem item) { return true; } - @Override - public void onBackPressed() { - if (!handlePodcastBackPressed()) { - super.onBackPressed(); - } - } - public static final int RESULT_SETTINGS = 15642; private void syncMenuItemUnreadOnly() { diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsActivity.java index 0664b66a5..6bb39072b 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsActivity.java @@ -70,6 +70,7 @@ public class SettingsActivity extends AppCompatActivity { public static final String CB_MARK_AS_READ_WHILE_SCROLLING_STRING = "cb_MarkAsReadWhileScrolling"; public static final String CB_SHOW_FAST_ACTIONS = "cb_ShowFastActions"; + public static final String CB_PREF_BACK_OPENS_DRAWER = "cb_prefBackButtonOpensDrawer"; public static final String CB_DISABLE_HOSTNAME_VERIFICATION_STRING = "cb_DisableHostnameVerification"; public static final String CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING = "cb_openInBrowserDirectly"; diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsFragment.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsFragment.java index fecc72c2f..3402a7e1a 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsFragment.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/SettingsFragment.java @@ -26,6 +26,7 @@ import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION; import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_RIGHT_ACTION; import static de.luhmer.owncloudnewsreader.SettingsActivity.SYNC_INTERVAL_IN_MINUTES_STRING_DEPRECATED; +import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_PREF_BACK_OPENS_DRAWER; import android.accounts.Account; import android.accounts.AccountManager; @@ -239,6 +240,7 @@ private void bindGeneralPreferences(DialogPreference.TargetFragment prefFrag) bindPreferenceBooleanToValue(prefFrag.findPreference(CB_MARK_AS_READ_WHILE_SCROLLING_STRING)); bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOW_FAST_ACTIONS)); bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING)); + bindPreferenceBooleanToValue(prefFrag.findPreference(CB_PREF_BACK_OPENS_DRAWER)); bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SORT_ORDER)); bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SEARCH_IN)); bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SWIPE_RIGHT_ACTION)); diff --git a/News-Android-App/src/main/res/values/strings.xml b/News-Android-App/src/main/res/values/strings.xml index a768bac92..f180a0252 100644 --- a/News-Android-App/src/main/res/values/strings.xml +++ b/News-Android-App/src/main/res/values/strings.xml @@ -211,6 +211,7 @@ Mark as read while scrolling Activate fast access functions Skip detailed view and open article in the browser + Back button opens drawer Accept Unknown Certificate? diff --git a/News-Android-App/src/main/res/xml/pref_general.xml b/News-Android-App/src/main/res/xml/pref_general.xml index 68320503e..0cb145f24 100644 --- a/News-Android-App/src/main/res/xml/pref_general.xml +++ b/News-Android-App/src/main/res/xml/pref_general.xml @@ -90,6 +90,11 @@ android:title="@string/pref_title_OpenInBrowserDirectly" app:iconSpaceReserved="false"/> + +