Keep screen on while video is playing. Closes https://github.com/austinhuang0131/barinsta/issues/490

This commit is contained in:
Ammar Githam 2021-03-20 13:33:37 +09:00
parent ee36ba8c35
commit 8e32958da6
7 changed files with 63 additions and 0 deletions

View File

@ -12,4 +12,7 @@ public class SliderCallbackAdapter implements SliderItemsAdapter.SliderCallback
@Override @Override
public void onPlayerPause(final int position) {} public void onPlayerPause(final int position) {}
@Override
public void onPlayerRelease(final int position) {}
} }

View File

@ -148,5 +148,7 @@ public final class SliderItemsAdapter extends ListAdapter<Media, SliderItemViewH
void onPlayerPlay(int position); void onPlayerPlay(int position);
void onPlayerPause(int position); void onPlayerPause(int position);
void onPlayerRelease(int position);
} }
} }

View File

@ -114,6 +114,13 @@ public class SliderVideoViewHolder extends SliderItemViewHolder {
sliderCallback.onPlayerPause(position); sliderCallback.onPlayerPause(position);
} }
} }
@Override
public void onRelease() {
if (sliderCallback != null) {
sliderCallback.onPlayerRelease(position);
}
}
}; };
final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight();
String videoUrl = null; String videoUrl = null;

View File

@ -15,4 +15,7 @@ public class VideoPlayerCallbackAdapter implements VideoPlayerViewHelper.VideoPl
@Override @Override
public void onPause() {} public void onPause() {}
@Override
public void onRelease() {}
} }

View File

@ -359,6 +359,9 @@ public class VideoPlayerViewHelper implements Player.EventListener {
// } // }
public void releasePlayer() { public void releasePlayer() {
if (videoPlayerCallback != null) {
videoPlayerCallback.onRelease();
}
if (player != null) { if (player != null) {
player.release(); player.release();
player = null; player = null;
@ -453,5 +456,7 @@ public class VideoPlayerViewHelper implements Player.EventListener {
void onPlay(); void onPlay();
void onPause(); void onPause();
void onRelease();
} }
} }

View File

@ -48,6 +48,7 @@ import androidx.core.content.PermissionChecker;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView; import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
@ -1062,15 +1063,28 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment im
@Override @Override
public void onPlayerPlay(final int position) { public void onPlayerPlay(final int position) {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.enabledKeepScreenOn(activity);
if (!detailsVisible || hasBeenToggled) return; if (!detailsVisible || hasBeenToggled) return;
showPlayerControls(); showPlayerControls();
} }
@Override @Override
public void onPlayerPause(final int position) { public void onPlayerPause(final int position) {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.disableKeepScreenOn(activity);
if (detailsVisible || hasBeenToggled) return; if (detailsVisible || hasBeenToggled) return;
toggleDetails(); toggleDetails();
} }
@Override
public void onPlayerRelease(final int position) {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.disableKeepScreenOn(activity);
}
}); });
binding.sliderParent.setAdapter(sliderItemsAdapter); binding.sliderParent.setAdapter(sliderItemsAdapter);
if (sliderPosition >= 0 && sliderPosition < media.getCarouselMedia().size()) { if (sliderPosition >= 0 && sliderPosition < media.getCarouselMedia().size()) {
@ -1218,10 +1232,27 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment im
@Override @Override
public void onPlay() { public void onPlay() {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.enabledKeepScreenOn(activity);
if (detailsVisible) { if (detailsVisible) {
new Handler().postDelayed(() -> toggleDetails(), DETAILS_HIDE_DELAY_MILLIS); new Handler().postDelayed(() -> toggleDetails(), DETAILS_HIDE_DELAY_MILLIS);
} }
} }
@Override
public void onPause() {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.disableKeepScreenOn(activity);
}
@Override
public void onRelease() {
final FragmentActivity activity = getActivity();
if (activity == null) return;
Utils.disableKeepScreenOn(activity);
}
}; };
final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight();
String videoUrl = null; String videoUrl = null;

View File

@ -359,4 +359,16 @@ public final class Utils {
} }
return drawable; return drawable;
} }
public static void enabledKeepScreenOn(@NonNull final Activity activity) {
final Window window = activity.getWindow();
if (window == null) return;
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
public static void disableKeepScreenOn(@NonNull final Activity activity) {
final Window window = activity.getWindow();
if (window == null) return;
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} }