Skip to content

Commit

Permalink
Extract isLandscape and isInMultiWindow to DeviceUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Mar 25, 2021
1 parent f1d92b3 commit 0dd4d56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void onServiceConnected(final Player connectedPlayer,
return;
}

if (isLandscape()) {
if (DeviceUtils.isLandscape(requireContext())) {
// If the video is playing but orientation changed
// let's make the video in fullscreen again
checkLandscape();
Expand Down Expand Up @@ -494,7 +494,7 @@ public void onClick(final View v) {
autoPlayEnabled = true; // forcefully start playing
if (PlayerHelper.getAutoplayType(requireContext())
== PlayerHelper.AutoplayType.AUTOPLAY_TYPE_NEVER_AND_START_IN_FULLSCREEN
&& !isLandscape()
&& !DeviceUtils.isLandscape(requireContext())
&& PlayerHelper.globalScreenOrientationLocked(requireContext())) {
// open directly in fullscreen TODO does it work for large-land layouts?
onScreenRotationButtonClicked();
Expand Down Expand Up @@ -1257,7 +1257,7 @@ public boolean onPreDraw() {
final DisplayMetrics metrics = getResources().getDisplayMetrics();

if (getView() != null) {
final int height = (isInMultiWindow()
final int height = (DeviceUtils.isInMultiWindow(activity)
? requireView()
: activity.getWindow().getDecorView()).getHeight();
setHeightThumbnail(height, metrics);
Expand All @@ -1280,7 +1280,7 @@ private void setHeightThumbnail() {
requireView().getViewTreeObserver().removeOnPreDrawListener(preDrawListener);

if (player != null && player.isFullscreen()) {
final int height = (isInMultiWindow()
final int height = (DeviceUtils.isInMultiWindow(activity)
? requireView()
: activity.getWindow().getDecorView()).getHeight();
// Height is zero when the view is not yet displayed like after orientation change
Expand Down Expand Up @@ -1871,13 +1871,14 @@ public void onScreenRotationButtonClicked() {
// from landscape to portrait every time.
// Just turn on fullscreen mode in landscape orientation
// or portrait & unlocked global orientation
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
if (DeviceUtils.isTablet(activity)
&& (!globalScreenOrientationLocked(activity) || isLandscape())) {
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
player.toggleFullscreen();
return;
}

final int newOrientation = isLandscape()
final int newOrientation = isLandscape
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

Expand Down Expand Up @@ -1949,15 +1950,17 @@ private void hideSystemUi() {
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

// In multiWindow mode status bar is not transparent for devices with cutout
// if I include this flag. So without it is better in this case
if (!isInMultiWindow()) {
final boolean isInMultiWindow = DeviceUtils.isInMultiWindow(activity);
if (!isInMultiWindow) {
visibility |= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
activity.getWindow().getDecorView().setSystemUiVisibility(visibility);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& (isInMultiWindow() || (player != null && player.isFullscreen()))) {
&& (isInMultiWindow || (player != null && player.isFullscreen()))) {
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
}
Expand Down Expand Up @@ -2029,15 +2032,6 @@ private void checkLandscape() {
}
}

public boolean isLandscape() {
return getResources().getDisplayMetrics().heightPixels < getResources()
.getDisplayMetrics().widthPixels;
}

private boolean isInMultiWindow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode();
}

/*
* Means that the player fragment was swiped away via BottomSheetLayout
* and is empty but ready for any new actions. See cleanUp()
Expand Down Expand Up @@ -2220,7 +2214,7 @@ public void onStateChanged(@NonNull final View bottomSheet, final int newState)
setOverlayElementsClickable(false);
hideSystemUiIfNeeded();
// Conditions when the player should be expanded to fullscreen
if (isLandscape()
if (DeviceUtils.isLandscape(requireContext())
&& player != null
&& player.isPlaying()
&& !player.isFullscreen()
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/org/schabi/newpipe/player/MainPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.schabi.newpipe.App;
import org.schabi.newpipe.databinding.PlayerBinding;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.ThemeHelper;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
Expand Down Expand Up @@ -214,11 +215,8 @@ public IBinder onBind(final Intent intent) {
boolean isLandscape() {
// DisplayMetrics from activity context knows about MultiWindow feature
// while DisplayMetrics from app context doesn't
final DisplayMetrics metrics = (player != null
&& player.getParentActivity() != null
? player.getParentActivity().getResources()
: getResources()).getDisplayMetrics();
return metrics.heightPixels < metrics.widthPixels;
return DeviceUtils.isLandscape(player != null && player.getParentActivity() != null
? player.getParentActivity() : this);
}

@Nullable
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import org.schabi.newpipe.App;
Expand Down Expand Up @@ -88,4 +89,13 @@ public static int spToPx(@Dimension(unit = Dimension.SP) final int sp,
sp,
context.getResources().getDisplayMetrics());
}

public static boolean isLandscape(final Context context) {
return context.getResources().getDisplayMetrics().heightPixels < context.getResources()
.getDisplayMetrics().widthPixels;
}

public static boolean isInMultiWindow(final AppCompatActivity activity) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static void openVideoDetailFragment(@NonNull final Context context,

// Starting directly in fullscreen if the previous player type was popup.
if (playerType == MainPlayer.PlayerType.POPUP
&& !detailFragment.isLandscape()
&& !DeviceUtils.isLandscape(context)
&& PlayerHelper.globalScreenOrientationLocked(context)) {
detailFragment.onScreenRotationButtonClicked();
}
Expand Down

0 comments on commit 0dd4d56

Please sign in to comment.