Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
feat(Hide ads): add Close fullscreen ads settings inotia00/ReVanced…
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Jun 10, 2024
1 parent d0fe7d0 commit f328da8
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 66 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class Settings extends BaseSettings {


// PreferenceScreen: Ads
public static final BooleanSetting HIDE_FULLSCREEN_ADS = new BooleanSetting("revanced_hide_fullscreen_ads", TRUE, true);
public static final BooleanSetting HIDE_GENERAL_ADS = new BooleanSetting("revanced_hide_general_ads", TRUE, true);
public static final BooleanSetting HIDE_MUSIC_ADS = new BooleanSetting("revanced_hide_music_ads", TRUE, true);
public static final BooleanSetting HIDE_PAID_PROMOTION_LABEL = new BooleanSetting("revanced_hide_paid_promotion_label", TRUE, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package app.revanced.integrations.shared.patches;

import static app.revanced.integrations.shared.utils.StringRef.str;
import static app.revanced.integrations.shared.utils.Utils.hideViewBy0dpUnderCondition;
import static app.revanced.integrations.shared.utils.Utils.showToastShort;

import android.view.View;
import android.widget.Button;

import java.lang.ref.WeakReference;
import java.util.Arrays;

import app.revanced.integrations.shared.settings.BaseSettings;
import app.revanced.integrations.shared.utils.Logger;
import app.revanced.integrations.shared.utils.Utils;

@SuppressWarnings("unused")
public class FullscreenAdsPatch {
private static final boolean hideFullscreenAdsEnabled = BaseSettings.HIDE_FULLSCREEN_ADS.get();
private static final boolean closeFullscreenAdsEnabled = BaseSettings.HIDE_FULLSCREEN_ADS_TYPE.get();
private static final boolean closeDialog = hideFullscreenAdsEnabled && closeFullscreenAdsEnabled;
private static final boolean disableDialog = hideFullscreenAdsEnabled && !closeFullscreenAdsEnabled;

private static WeakReference<Button> buttonRef = new WeakReference<>(null);

public static boolean disableFullscreenAds(int code) {
if (!disableDialog) return false;

final DialogType dialogType = DialogType.getDialogType(code);
final String dialogName = dialogType.name();
Logger.printDebug(() -> "DialogType: " + dialogName);

// This method is also invoked in the 'Create new playlist' dialog,
// in which case the DialogType is {@code DialogType.ALERT}.
if (dialogType == DialogType.ALERT) return false;

showToastShort(str("revanced_hide_fullscreen_ads_blocked_success", dialogName));
return true;
}

public static void setCloseButton(final Button button) {
if (!closeDialog) return;
buttonRef = new WeakReference<>(button);
}

public static void closeFullscreenAds() {
if (!closeDialog) return;

Utils.runOnMainThreadDelayed(() -> {
final Button button = buttonRef.get();
if (button == null) return;
button.callOnClick();
showToastShort(str("revanced_hide_fullscreen_ads_closed_success"));
}, 100);
}

public static void hideFullscreenAds(View view) {
hideViewBy0dpUnderCondition(
hideFullscreenAdsEnabled,
view
);
}

private enum DialogType {
NULL(0),
ALERT(1),
FULLSCREEN(2),
LAYOUT_FULLSCREEN(3);

private final int code;

DialogType(int code) {
this.code = code;
}

private static DialogType getDialogType(int code){
return Arrays.stream(values())
.filter(val -> code == val.code)
.findFirst()
.orElse(null);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app.revanced.integrations.shared.patches.components;

import static app.revanced.integrations.shared.patches.FullscreenAdsPatch.closeFullscreenAds;

import androidx.annotation.Nullable;

import app.revanced.integrations.shared.settings.BaseSettings;

@SuppressWarnings("unused")
public final class FullscreenAdsFilter extends Filter {

public FullscreenAdsFilter() {
addPathCallbacks(
new StringFilterGroup(
BaseSettings.HIDE_FULLSCREEN_ADS,
"_interstitial"
)
);
}

@Override
public boolean isFiltered(String path, @Nullable String identifier, String allValue, byte[] protobufBufferArray,
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {
if (path.contains("|ImageType|")) closeFullscreenAds();

return false; // Do not actually filter the fullscreen ad otherwise it will leave a dimmed screen.
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.revanced.integrations.shared.settings;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

/**
* Settings shared across multiple apps.
Expand All @@ -17,4 +18,7 @@ public class BaseSettings {
public static final BooleanSetting ENABLE_DEBUG_BUFFER_LOGGING = new BooleanSetting("revanced_enable_debug_buffer_logging", FALSE);
public static final BooleanSetting SETTINGS_INITIALIZED = new BooleanSetting("revanced_settings_initialized", FALSE, false, false);

public static final BooleanSetting HIDE_FULLSCREEN_ADS = new BooleanSetting("revanced_hide_fullscreen_ads", TRUE, true);
public static final BooleanSetting HIDE_FULLSCREEN_ADS_TYPE = new BooleanSetting("revanced_hide_fullscreen_ads_type", TRUE, true);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@

import android.view.View;

import app.revanced.integrations.shared.utils.Logger;
import app.revanced.integrations.shared.utils.Utils;
import app.revanced.integrations.youtube.settings.Settings;

@SuppressWarnings("unused")
public class AdsPatch {
private static final String DIALOG_DATA_EXCEPTION = "ALERT";
private static final Boolean hideFullscreenAdsEnabled = Settings.HIDE_FULLSCREEN_ADS.get();
private static final Boolean hideGeneralAdsEnabled = Settings.HIDE_GENERAL_ADS.get();
private static final Boolean hideGetPremiumAdsEnabled = Settings.HIDE_GET_PREMIUM.get();
private static final Boolean hideVideoAdsEnabled = Settings.HIDE_VIDEO_ADS.get();

// region [Hide ads] patch

/**
* Injection point.
* Hide the view, which shows ads in the homepage.
Expand All @@ -28,25 +22,6 @@ public static void hideAdAttributionView(View view) {
hideViewBy0dpUnderCondition(hideGeneralAdsEnabled, view);
}

/**
* Injection point.
*/
public static boolean hideFullscreenAds(Object object) {
final String dialogData = object.toString();
Logger.printDebug(() -> dialogData);
return hideFullscreenAdsEnabled && !Utils.containsAny(dialogData, DIALOG_DATA_EXCEPTION);
}

/**
* Injection point.
* Hide the view, which shows fullscreen ads in the homepage.
*
* @param view The view, which shows fullscreen ads.
*/
public static void hideFullscreenAds(View view) {
hideViewBy0dpUnderCondition(hideFullscreenAdsEnabled, view);
}

public static boolean hideGetPremium() {
return hideGetPremiumAdsEnabled;
}
Expand All @@ -68,6 +43,4 @@ public static boolean hideVideoAds(boolean original) {
return !hideVideoAdsEnabled && original;
}

// endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
@SuppressWarnings("unused")
public class Settings extends BaseSettings {
// PreferenceScreen: Ads
public static final BooleanSetting HIDE_FULLSCREEN_ADS = new BooleanSetting("revanced_hide_fullscreen_ads", TRUE, true);
public static final BooleanSetting HIDE_GENERAL_ADS = new BooleanSetting("revanced_hide_general_ads", TRUE);
public static final BooleanSetting HIDE_GET_PREMIUM = new BooleanSetting("revanced_hide_get_premium", TRUE, true);
public static final BooleanSetting HIDE_MERCHANDISE_SHELF = new BooleanSetting("revanced_hide_merchandise_shelf", TRUE);
Expand Down

0 comments on commit f328da8

Please sign in to comment.