Skip to content

Commit fd68a1e

Browse files
committed
Extract isLandscape and isInMultiWindow to DeviceUtils
1 parent 64082e1 commit fd68a1e

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

+12-18
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void onServiceConnected(final Player connectedPlayer,
219219
return;
220220
}
221221

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

12591259
if (getView() != null) {
1260-
final int height = (isInMultiWindow()
1260+
final int height = (DeviceUtils.isInMultiWindow(activity)
12611261
? requireView()
12621262
: activity.getWindow().getDecorView()).getHeight();
12631263
setHeightThumbnail(height, metrics);
@@ -1280,7 +1280,7 @@ private void setHeightThumbnail() {
12801280
requireView().getViewTreeObserver().removeOnPreDrawListener(preDrawListener);
12811281

12821282
if (player != null && player.isFullscreen()) {
1283-
final int height = (isInMultiWindow()
1283+
final int height = (DeviceUtils.isInMultiWindow(activity)
12841284
? requireView()
12851285
: activity.getWindow().getDecorView()).getHeight();
12861286
// Height is zero when the view is not yet displayed like after orientation change
@@ -1871,13 +1871,14 @@ public void onScreenRotationButtonClicked() {
18711871
// from landscape to portrait every time.
18721872
// Just turn on fullscreen mode in landscape orientation
18731873
// or portrait & unlocked global orientation
1874+
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
18741875
if (DeviceUtils.isTablet(activity)
1875-
&& (!globalScreenOrientationLocked(activity) || isLandscape())) {
1876+
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
18761877
player.toggleFullscreen();
18771878
return;
18781879
}
18791880

1880-
final int newOrientation = isLandscape()
1881+
final int newOrientation = isLandscape
18811882
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
18821883
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
18831884

@@ -1949,15 +1950,17 @@ private void hideSystemUi() {
19491950
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
19501951
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
19511952
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
1953+
19521954
// In multiWindow mode status bar is not transparent for devices with cutout
19531955
// if I include this flag. So without it is better in this case
1954-
if (!isInMultiWindow()) {
1956+
final boolean isInMultiWindow = DeviceUtils.isInMultiWindow(activity);
1957+
if (!isInMultiWindow) {
19551958
visibility |= View.SYSTEM_UI_FLAG_FULLSCREEN;
19561959
}
19571960
activity.getWindow().getDecorView().setSystemUiVisibility(visibility);
19581961

19591962
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
1960-
&& (isInMultiWindow() || (player != null && player.isFullscreen()))) {
1963+
&& (isInMultiWindow || (player != null && player.isFullscreen()))) {
19611964
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
19621965
activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
19631966
}
@@ -2029,15 +2032,6 @@ private void checkLandscape() {
20292032
}
20302033
}
20312034

2032-
public boolean isLandscape() {
2033-
return getResources().getDisplayMetrics().heightPixels < getResources()
2034-
.getDisplayMetrics().widthPixels;
2035-
}
2036-
2037-
private boolean isInMultiWindow() {
2038-
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode();
2039-
}
2040-
20412035
/*
20422036
* Means that the player fragment was swiped away via BottomSheetLayout
20432037
* and is empty but ready for any new actions. See cleanUp()
@@ -2220,7 +2214,7 @@ public void onStateChanged(@NonNull final View bottomSheet, final int newState)
22202214
setOverlayElementsClickable(false);
22212215
hideSystemUiIfNeeded();
22222216
// Conditions when the player should be expanded to fullscreen
2223-
if (isLandscape()
2217+
if (DeviceUtils.isLandscape(requireContext())
22242218
&& player != null
22252219
&& player.isPlaying()
22262220
&& !player.isFullscreen()

app/src/main/java/org/schabi/newpipe/player/MainPlayer.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.content.Intent;
2525
import android.os.Binder;
2626
import android.os.IBinder;
27-
import android.util.DisplayMetrics;
2827
import android.util.Log;
2928
import android.view.LayoutInflater;
3029
import android.view.View;
@@ -36,6 +35,7 @@
3635

3736
import org.schabi.newpipe.App;
3837
import org.schabi.newpipe.databinding.PlayerBinding;
38+
import org.schabi.newpipe.util.DeviceUtils;
3939
import org.schabi.newpipe.util.ThemeHelper;
4040

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

224221
@Nullable

app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import androidx.annotation.Dimension;
1313
import androidx.annotation.NonNull;
14+
import androidx.appcompat.app.AppCompatActivity;
1415
import androidx.core.content.ContextCompat;
1516

1617
import org.schabi.newpipe.App;
@@ -88,4 +89,13 @@ public static int spToPx(@Dimension(unit = Dimension.SP) final int sp,
8889
sp,
8990
context.getResources().getDisplayMetrics());
9091
}
92+
93+
public static boolean isLandscape(final Context context) {
94+
return context.getResources().getDisplayMetrics().heightPixels < context.getResources()
95+
.getDisplayMetrics().widthPixels;
96+
}
97+
98+
public static boolean isInMultiWindow(final AppCompatActivity activity) {
99+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode();
100+
}
91101
}

app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public static void openVideoDetailFragment(@NonNull final Context context,
367367

368368
// Starting directly in fullscreen if the previous player type was popup.
369369
if (playerType == MainPlayer.PlayerType.POPUP
370-
&& !detailFragment.isLandscape()
370+
&& !DeviceUtils.isLandscape(context)
371371
&& PlayerHelper.globalScreenOrientationLocked(context)) {
372372
detailFragment.onScreenRotationButtonClicked();
373373
}

0 commit comments

Comments
 (0)