Browse Source

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

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
8e32958da6
  1. 3
      app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java
  2. 2
      app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java
  3. 7
      app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java
  4. 3
      app/src/main/java/awais/instagrabber/customviews/VideoPlayerCallbackAdapter.java
  5. 5
      app/src/main/java/awais/instagrabber/customviews/VideoPlayerViewHelper.java
  6. 31
      app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java
  7. 12
      app/src/main/java/awais/instagrabber/utils/Utils.java

3
app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java

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

2
app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java

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

7
app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java

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

3
app/src/main/java/awais/instagrabber/customviews/VideoPlayerCallbackAdapter.java

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

5
app/src/main/java/awais/instagrabber/customviews/VideoPlayerViewHelper.java

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

31
app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java

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

12
app/src/main/java/awais/instagrabber/utils/Utils.java

@ -359,4 +359,16 @@ public final class Utils {
}
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);
}
}
Loading…
Cancel
Save