fix(YouTube - Miniplayer): Patched app crashes after first launch or clearing data

This commit is contained in:
inotia00 2024-12-31 20:47:37 +09:00
parent 8d045bc618
commit 74d39ff034

View File

@ -12,6 +12,8 @@ import static app.revanced.extension.youtube.utils.ExtendedUtils.IS_19_26_OR_GRE
import static app.revanced.extension.youtube.utils.ExtendedUtils.IS_19_29_OR_GREATER; import static app.revanced.extension.youtube.utils.ExtendedUtils.IS_19_29_OR_GREATER;
import static app.revanced.extension.youtube.utils.ExtendedUtils.validateValue; import static app.revanced.extension.youtube.utils.ExtendedUtils.validateValue;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -82,11 +84,24 @@ public final class MiniplayerPatch {
} }
} }
private static final int MINIPLAYER_SIZE; private static int MINIPLAYER_SIZE = 0;
static { static {
setMiniPlayerSize();
}
private static void setMiniPlayerSize() {
try {
Context context = Utils.getContext();
if (context == null) {
return;
}
Resources resources = context.getResources();
if (resources == null) {
return;
}
// YT appears to use the device screen dip width, plus an unknown fixed horizontal padding size. // YT appears to use the device screen dip width, plus an unknown fixed horizontal padding size.
DisplayMetrics displayMetrics = Utils.getContext().getResources().getDisplayMetrics(); DisplayMetrics displayMetrics = resources.getDisplayMetrics();
final int deviceDipWidth = (int) (displayMetrics.widthPixels / displayMetrics.density); final int deviceDipWidth = (int) (displayMetrics.widthPixels / displayMetrics.density);
// YT seems to use a minimum height to calculate the minimum miniplayer width based on the video. // YT seems to use a minimum height to calculate the minimum miniplayer width based on the video.
@ -116,15 +131,9 @@ public final class MiniplayerPatch {
} }
MINIPLAYER_SIZE = dipWidth; MINIPLAYER_SIZE = dipWidth;
} catch (Exception ex) {
final int opacity = validateValue( Logger.printException(() -> "setMiniPlayerSize failure", ex);
Settings.MINIPLAYER_OPACITY, }
0,
100,
"revanced_miniplayer_opacity_invalid_toast"
);
OPACITY_LEVEL = (opacity * 255) / 100;
} }
/** /**
@ -175,6 +184,17 @@ public final class MiniplayerPatch {
private static final int OPACITY_LEVEL; private static final int OPACITY_LEVEL;
static {
final int opacity = validateValue(
Settings.MINIPLAYER_OPACITY,
0,
100,
"revanced_miniplayer_opacity_invalid_toast"
);
OPACITY_LEVEL = (opacity * 255) / 100;
}
public static final class MiniplayerHorizontalDragAvailability implements Setting.Availability { public static final class MiniplayerHorizontalDragAvailability implements Setting.Availability {
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
@ -293,8 +313,13 @@ public final class MiniplayerPatch {
*/ */
public static int setMiniplayerDefaultSize(int original) { public static int setMiniplayerDefaultSize(int original) {
if (CURRENT_TYPE.isModern()) { if (CURRENT_TYPE.isModern()) {
if (MINIPLAYER_SIZE == 0) {
setMiniPlayerSize();
}
if (MINIPLAYER_SIZE != 0) {
return MINIPLAYER_SIZE; return MINIPLAYER_SIZE;
} }
}
return original; return original;
} }