Lock bottom sheet when playing video

This commit is contained in:
ByteHamster 2020-03-23 10:46:55 +01:00
parent e417f4a5f8
commit cc3e39a223
5 changed files with 104 additions and 12 deletions

View File

@ -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 {

View File

@ -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();

View File

@ -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!");

View File

@ -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<V extends View> extends BottomSheetBehavior<V> {
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;
}
}

View File

@ -22,11 +22,12 @@
tools:background="@android:color/holo_red_dark" />
<FrameLayout
android:elevation="8dp"
android:id="@+id/audioplayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
app:layout_behavior="de.danoeh.antennapod.view.LockableBottomSheetBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>