diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index 7a72ea7b..3804ffe2 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -19,13 +19,12 @@ import app.revanced.integrations.utils.StringRef; public enum SettingsEnum { //Download Settings - // TODO: DOWNLOAD_PATH("revanced_download_path", STRING, Environment.getExternalStorageDirectory().getPath() + "/Download"), - DOWNLOADS_BUTTON_SHOWN("revanced_downloads_enabled", BOOLEAN, TRUE, true), + DOWNLOADS_BUTTON_SHOWN("revanced_downloads_enabled", BOOLEAN, TRUE), DOWNLOADS_PACKAGE_NAME("revanced_downloads_package_name", STRING, "org.schabi.newpipe" /* NewPipe */, parents(DOWNLOADS_BUTTON_SHOWN)), // Copy video URL settings - COPY_VIDEO_URL_BUTTON_SHOWN("revanced_copy_video_url_enabled", BOOLEAN, TRUE, true), - COPY_VIDEO_URL_TIMESTAMP_BUTTON_SHOWN("revanced_copy_video_url_timestamp_enabled", BOOLEAN, TRUE, true), + COPY_VIDEO_URL_BUTTON_SHOWN("revanced_copy_video_url_enabled", BOOLEAN, TRUE), + COPY_VIDEO_URL_TIMESTAMP_BUTTON_SHOWN("revanced_copy_video_url_timestamp_enabled", BOOLEAN, TRUE), // Video settings OLD_STYLE_VIDEO_QUALITY_PLAYER_SETTINGS("revanced_use_old_style_quality_settings", BOOLEAN, TRUE), diff --git a/app/src/main/java/app/revanced/integrations/videoplayer/BottomControlButton.java b/app/src/main/java/app/revanced/integrations/videoplayer/BottomControlButton.java index 60923d9f..fe27fc8f 100644 --- a/app/src/main/java/app/revanced/integrations/videoplayer/BottomControlButton.java +++ b/app/src/main/java/app/revanced/integrations/videoplayer/BottomControlButton.java @@ -1,66 +1,64 @@ package app.revanced.integrations.videoplayer; -import android.support.constraint.ConstraintLayout; import android.view.View; +import android.view.ViewGroup; import android.view.animation.Animation; import android.widget.ImageView; - -import java.lang.ref.WeakReference; - +import androidx.annotation.NonNull; +import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; +import java.lang.ref.WeakReference; +import java.util.Objects; + public abstract class BottomControlButton { - WeakReference button = new WeakReference<>(null); - ConstraintLayout constraintLayout; - Boolean isButtonEnabled; - Boolean isShowing; + private static final Animation fadeIn = ReVancedUtils.getResourceAnimation("fade_in"); + private static final Animation fadeOut = ReVancedUtils.getResourceAnimation("fade_out"); + private final WeakReference buttonRef; + private final SettingsEnum setting; + protected boolean isVisible; - private Animation fadeIn; - private Animation fadeOut; - - public BottomControlButton(Object obj, String viewId, Boolean isEnabled, View.OnClickListener onClickListener) { - try { - LogHelper.printDebug(() -> "Initializing button with id: " + viewId); - constraintLayout = (ConstraintLayout) obj; - isButtonEnabled = isEnabled; - - ImageView imageView = constraintLayout.findViewById(ReVancedUtils.getResourceIdentifier(viewId, "id")); - if (imageView == null) { - LogHelper.printException(() -> "Couldn't find ImageView with id: " + viewId); - return; - } - imageView.setOnClickListener(onClickListener); - button = new WeakReference<>(imageView); - - fadeIn = ReVancedUtils.getResourceAnimation("fade_in"); - fadeOut = ReVancedUtils.getResourceAnimation("fade_out"); - fadeIn.setDuration(ReVancedUtils.getResourceInteger("fade_duration_fast")); - fadeOut.setDuration(ReVancedUtils.getResourceInteger("fade_duration_scheduled")); - - isShowing = true; - setVisibility(false); - } catch (Exception e) { - LogHelper.printException(() -> "Failed to initialize button with id: " + viewId, e); - } + static { + // TODO: check if these durations are correct. + fadeIn.setDuration(ReVancedUtils.getResourceInteger("fade_duration_fast")); + fadeOut.setDuration(ReVancedUtils.getResourceInteger("fade_duration_scheduled")); } - public void setVisibility(boolean showing) { - if (isShowing == showing) return; + public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull String imageViewButtonId, + @NonNull SettingsEnum booleanSetting, @NonNull View.OnClickListener onClickListener) { + LogHelper.printDebug(() -> "Initializing button: " + imageViewButtonId); - isShowing = showing; - ImageView imageView = button.get(); - - if (constraintLayout == null || imageView == null) - return; - - if (showing && isButtonEnabled) { - LogHelper.printDebug(() -> "Fading in"); - imageView.setVisibility(View.VISIBLE); - imageView.startAnimation(fadeIn); + if (booleanSetting.returnType != SettingsEnum.ReturnType.BOOLEAN) { + throw new IllegalArgumentException(); } - else if (imageView.getVisibility() == View.VISIBLE) { - LogHelper.printDebug(() -> "Fading out"); + + setting = booleanSetting; + + // Create the button. + ImageView imageView = Objects.requireNonNull(bottomControlsViewGroup.findViewById( + ReVancedUtils.getResourceIdentifier(imageViewButtonId, "id") + )); + imageView.setOnClickListener(onClickListener); + imageView.setVisibility(View.GONE); + + buttonRef = new WeakReference<>(imageView); + } + + public void setVisibility(boolean visible) { + if (isVisible == visible) return; + isVisible = visible; + + ImageView imageView = buttonRef.get(); + if (imageView == null) { + return; + } + + imageView.clearAnimation(); + if (visible && setting.getBoolean()) { + imageView.startAnimation(fadeIn); + imageView.setVisibility(View.VISIBLE); + } else if (imageView.getVisibility() == View.VISIBLE) { imageView.startAnimation(fadeOut); imageView.setVisibility(View.GONE); } diff --git a/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlButton.java b/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlButton.java index 38a62114..23bcd151 100644 --- a/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlButton.java +++ b/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlButton.java @@ -1,25 +1,40 @@ package app.revanced.integrations.videoplayer; +import android.view.ViewGroup; + +import androidx.annotation.Nullable; import app.revanced.integrations.patches.CopyVideoUrlPatch; import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.utils.LogHelper; public class CopyVideoUrlButton extends BottomControlButton { - public static CopyVideoUrlButton instance; + @Nullable + private static CopyVideoUrlButton instance; - public CopyVideoUrlButton(Object obj) { + public CopyVideoUrlButton(ViewGroup viewGroup) { super( - obj, + viewGroup, "copy_video_url_button", - SettingsEnum.COPY_VIDEO_URL_BUTTON_SHOWN.getBoolean(), + SettingsEnum.COPY_VIDEO_URL_BUTTON_SHOWN, view -> CopyVideoUrlPatch.copyUrl(false) ); } + /** + * Injection point. + */ public static void initializeButton(Object obj) { - instance = new CopyVideoUrlButton(obj); + try { + instance = new CopyVideoUrlButton((ViewGroup) obj); + } catch (Exception ex) { + LogHelper.printException(() -> "initializeButton failure", ex); + } } + /** + * Injection point. + */ public static void changeVisibility(boolean showing) { if (instance != null) instance.setVisibility(showing); } diff --git a/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlTimestampButton.java b/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlTimestampButton.java index 482ace14..3a94344d 100644 --- a/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlTimestampButton.java +++ b/app/src/main/java/app/revanced/integrations/videoplayer/CopyVideoUrlTimestampButton.java @@ -1,24 +1,40 @@ package app.revanced.integrations.videoplayer; +import android.view.ViewGroup; + +import androidx.annotation.Nullable; + import app.revanced.integrations.patches.CopyVideoUrlPatch; import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.utils.LogHelper; public class CopyVideoUrlTimestampButton extends BottomControlButton { - public static CopyVideoUrlTimestampButton instance; + @Nullable + private static CopyVideoUrlTimestampButton instance; - public CopyVideoUrlTimestampButton(Object obj) { + public CopyVideoUrlTimestampButton(ViewGroup bottomControlsViewGroup) { super( - obj, + bottomControlsViewGroup, "copy_video_url_timestamp_button", - SettingsEnum.COPY_VIDEO_URL_TIMESTAMP_BUTTON_SHOWN.getBoolean(), + SettingsEnum.COPY_VIDEO_URL_TIMESTAMP_BUTTON_SHOWN, view -> CopyVideoUrlPatch.copyUrl(true) ); } - public static void initializeButton(Object obj) { - instance = new CopyVideoUrlTimestampButton(obj); + /** + * Injection point. + */ + public static void initializeButton(Object bottomControlsViewGroup) { + try { + instance = new CopyVideoUrlTimestampButton((ViewGroup) bottomControlsViewGroup); + } catch (Exception ex) { + LogHelper.printException(() -> "initializeButton failure", ex); + } } + /** + * Injection point. + */ public static void changeVisibility(boolean showing) { if (instance != null) instance.setVisibility(showing); } diff --git a/app/src/main/java/app/revanced/integrations/videoplayer/DownloadButton.java b/app/src/main/java/app/revanced/integrations/videoplayer/DownloadButton.java index 4b915dc0..dcb581a2 100644 --- a/app/src/main/java/app/revanced/integrations/videoplayer/DownloadButton.java +++ b/app/src/main/java/app/revanced/integrations/videoplayer/DownloadButton.java @@ -3,6 +3,9 @@ package app.revanced.integrations.videoplayer; import android.content.Intent; import android.content.pm.PackageManager; import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.Nullable; import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.settings.SettingsEnum; @@ -11,21 +14,32 @@ import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.StringRef; public class DownloadButton extends BottomControlButton { - public static DownloadButton instance; + @Nullable + private static DownloadButton instance; - public DownloadButton(Object obj) { + public DownloadButton(ViewGroup viewGroup) { super( - obj, + viewGroup, "download_button", - SettingsEnum.DOWNLOADS_BUTTON_SHOWN.getBoolean(), + SettingsEnum.DOWNLOADS_BUTTON_SHOWN, DownloadButton::onDownloadClick ); } + /** + * Injection point. + */ public static void initializeButton(Object obj) { - instance = new DownloadButton(obj); + try { + instance = new DownloadButton((ViewGroup) obj); + } catch (Exception ex) { + LogHelper.printException(() -> "initializeButton failure", ex); + } } + /** + * Injection point. + */ public static void changeVisibility(boolean showing) { if (instance != null) instance.setVisibility(showing); } @@ -38,7 +52,6 @@ public class DownloadButton extends BottomControlButton { boolean packageEnabled = false; try { - assert context != null; packageEnabled = context.getPackageManager().getApplicationInfo(downloaderPackageName, 0).enabled; } catch (PackageManager.NameNotFoundException error) { LogHelper.printDebug(() -> "Downloader could not be found: " + error);