From cc3e39a2231d213c7f08ae901590b7dcbcf9d37d Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 23 Mar 2020 10:46:55 +0100 Subject: [PATCH] Lock bottom sheet when playing video --- .../antennapod/activity/MainActivity.java | 15 ++-- .../fragment/AudioPlayerFragment.java | 4 +- .../fragment/ExternalPlayerFragment.java | 6 +- .../view/LockableBottomSheetBehavior.java | 88 +++++++++++++++++++ app/src/main/res/layout/main.xml | 3 +- 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/view/LockableBottomSheetBehavior.java diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index c8b10751d..05b167201 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -42,6 +42,7 @@ import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.fragment.SubscriptionFragment; import de.danoeh.antennapod.fragment.TransitionEffect; import de.danoeh.antennapod.preferences.PreferenceUpgrader; +import de.danoeh.antennapod.view.LockableBottomSheetBehavior; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.Validate; import org.greenrobot.eventbus.EventBus; @@ -68,7 +69,7 @@ public class MainActivity extends CastEnabledActivity { private DrawerLayout drawerLayout; private View navDrawer; private ActionBarDrawerToggle drawerToggle; - private BottomSheetBehavior sheetBehavior; + private LockableBottomSheetBehavior sheetBehavior; private long lastBackButtonPressTime = 0; @NonNull @@ -122,7 +123,7 @@ public class MainActivity extends CastEnabledActivity { checkFirstLaunch(); PreferenceUpgrader.checkUpgrades(this); View bottomSheet = findViewById(R.id.audioplayerFragment); - sheetBehavior = BottomSheetBehavior.from(bottomSheet); + sheetBehavior = (LockableBottomSheetBehavior) BottomSheetBehavior.from(bottomSheet); sheetBehavior.setPeekHeight((int) getResources().getDimension(R.dimen.external_player_height)); sheetBehavior.setHideable(false); sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @@ -172,12 +173,8 @@ public class MainActivity extends CastEnabledActivity { return drawerLayout != null && navDrawer != null && drawerLayout.isDrawerOpen(navDrawer); } - public void expandBottomSheet() { - sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); - } - - public void collapseBottomSheet() { - sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); + public LockableBottomSheetBehavior getBottomSheet() { + return sheetBehavior; } public void loadFragment(String tag, Bundle args) { @@ -370,7 +367,7 @@ public class MainActivity extends CastEnabledActivity { if (isDrawerOpen()) { drawerLayout.closeDrawer(navDrawer); } else if (sheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) { - collapseBottomSheet(); + sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } else if (getSupportFragmentManager().getBackStackEntryCount() != 0) { super.onBackPressed(); } else { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java index 7d8500f44..d1c073cac 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java @@ -18,6 +18,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.google.android.material.bottomsheet.BottomSheetBehavior; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.event.FavoritesEvent; @@ -86,7 +87,8 @@ public class AudioPlayerFragment extends Fragment implements View root = inflater.inflate(R.layout.audioplayer_fragment, container, false); toolbar = root.findViewById(R.id.toolbar); toolbar.setTitle(""); - toolbar.setNavigationOnClickListener(v -> ((MainActivity) getActivity()).collapseBottomSheet()); + toolbar.setNavigationOnClickListener(v -> + ((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED)); toolbar.setOnMenuItemClickListener(this); setupOptionsMenu(); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java index fa753aac8..57193e08b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java @@ -15,6 +15,7 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.bottomsheet.BottomSheetBehavior; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; @@ -69,7 +70,7 @@ public class ExternalPlayerFragment extends Fragment { if (controller != null && controller.getMedia() != null) { if (controller.getMedia().getMediaType() == MediaType.AUDIO) { - ((MainActivity) getActivity()).expandBottomSheet(); + ((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_EXPANDED); } else { Intent intent = PlaybackService.getPlayerActivityIntent(getActivity(), controller.getMedia()); startActivity(intent); @@ -236,8 +237,11 @@ public class ExternalPlayerFragment extends Fragment { fragmentLayout.setVisibility(View.VISIBLE); if (controller != null && controller.isPlayingVideoLocally()) { butPlay.setVisibility(View.GONE); + ((MainActivity) getActivity()).getBottomSheet().setLocked(true); + ((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED); } else { butPlay.setVisibility(View.VISIBLE); + ((MainActivity) getActivity()).getBottomSheet().setLocked(false); } } else { Log.w(TAG, "loadMediaInfo was called while the media object of playbackService was null!"); diff --git a/app/src/main/java/de/danoeh/antennapod/view/LockableBottomSheetBehavior.java b/app/src/main/java/de/danoeh/antennapod/view/LockableBottomSheetBehavior.java new file mode 100644 index 000000000..f4c073fc6 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/LockableBottomSheetBehavior.java @@ -0,0 +1,88 @@ +package de.danoeh.antennapod.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.bottomsheet.BottomSheetBehavior; + +/** + * Based on https://stackoverflow.com/a/40798214 + */ +public class LockableBottomSheetBehavior extends BottomSheetBehavior { + + private boolean mLocked = false; + + public LockableBottomSheetBehavior() {} + + public LockableBottomSheetBehavior(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public void setLocked(boolean locked) { + mLocked = locked; + } + + @Override + public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) { + boolean handled = false; + + if (!mLocked) { + handled = super.onInterceptTouchEvent(parent, child, event); + } + + return handled; + } + + @Override + public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) { + boolean handled = false; + + if (!mLocked) { + handled = super.onTouchEvent(parent, child, event); + } + + return handled; + } + + @Override + public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, + View target, int nestedScrollAxes) { + boolean handled = false; + + if (!mLocked) { + handled = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); + } + + return handled; + } + + @Override + public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, + int dx, int dy, int[] consumed) { + if (!mLocked) { + super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed); + } + } + + @Override + public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) { + if (!mLocked) { + super.onStopNestedScroll(coordinatorLayout, child, target); + } + } + + @Override + public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, + float velocityX, float velocityY) { + boolean handled = false; + + if (!mLocked) { + handled = super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY); + } + + return handled; + + } +} diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index 90088b792..60b9623bc 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -22,11 +22,12 @@ tools:background="@android:color/holo_red_dark" /> + app:layout_behavior="de.danoeh.antennapod.view.LockableBottomSheetBehavior" />