Skip to content

Commit

Permalink
Merge pull request #134 from wanam/add_prefs
Browse files Browse the repository at this point in the history
Add prefs UI to make some features optional
  • Loading branch information
wanam authored Feb 11, 2023
2 parents 02e6f71 + 0f44736 commit 0b9698d
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 200 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdk 27
targetSdk 31
versionCode 505
versionName "5.0.5"
versionName "5.0.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -30,7 +30,10 @@ android {
dependencies {
compileOnly 'de.robv.android.xposed:api:82'
compileOnly 'de.robv.android.xposed:api:82:sources'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.preference:preference:1.1.1'

// used for debugging only
// implementation 'org.apache.commons:commons-lang3:3.12.0'
}
}
11 changes: 7 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
android:theme="@style/Theme.SettingApp">
<meta-data
android:name="xposedmodule"
android:value="true" />
<meta-data
android:name="xposedminversion"
android:value="82+" />
android:value="93+" />
<meta-data
android:name="xposeddescription"
android:value="YouTube AdAway by Wanam" />

<meta-data
android:name="xposedsharedprefs"
android:value="true" />

<activity
android:name="ma.wanam.youtubeadaway.MainActivity"
android:name=".SettingsActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
Expand Down
71 changes: 30 additions & 41 deletions app/src/main/java/ma/wanam/youtubeadaway/BFAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import ma.wanam.youtubeadaway.utils.Class3C;
import ma.wanam.youtubeadaway.utils.Constants;

public class BFAsync extends AsyncTask<XC_LoadPackage.LoadPackageParam, Void, Boolean> {
private boolean DEBUG = BuildConfig.DEBUG;
Expand Down Expand Up @@ -55,24 +56,7 @@ public Handler getHandler() {
"_ad_with",
"landscape_image_wide_button_layout",
"carousel_ad",
"post_base_wrapper",
"community_guidelines",
"sponsorships_comments_upsell",
"member_recognition_shelf",
"compact_banner",
"in_feed_survey",
"medical_panel",
"paid_content_overlay",
"product_carousel",
"publisher_transparency_panel",
"single_item_information_panel",
"horizontal_video_shelf",
"post_shelf",
"channel_guidelines_entry_banner",
"official_card",
"cta_shelf_card",
"expandable_metadata",
"cell_divider"
"paid_content_overlay"
})).append(").*").toString();

private static final String filterIgnore = new StringBuffer().append(".*(").append(String.join("|", new String[]{
Expand All @@ -89,36 +73,41 @@ public Handler getHandler() {
protected Boolean doInBackground(XC_LoadPackage.LoadPackageParam... params) {
ClassLoader cl = params[0].classLoader;

XposedHelpers.findAndHookMethod("com.google.android.apps.youtube.app.watchwhile.WatchWhileActivity", cl, "onBackPressed", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (isAtTopOfView) {
XposedHelpers.callMethod(param.thisObject, "finish");
} else {
getHandler().removeCallbacksAndMessages(null);
getHandler().postDelayed(() -> isAtTopOfView = true, 1000);
}
}
});

XposedHelpers.findAndHookMethod("android.support.v7.widget.RecyclerView", cl, "stopNestedScroll",
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (params[0].packageName.equals(Constants.GOOGLE_YOUTUBE_PACKAGE)) {
XposedHelpers.findAndHookMethod("com.google.android.apps.youtube.app.watchwhile.WatchWhileActivity", cl, "onBackPressed", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (isAtTopOfView) {
XposedHelpers.callMethod(param.thisObject, "finish");
} else {
getHandler().removeCallbacksAndMessages(null);
isAtTopOfView = false;
getHandler().postDelayed(
() -> isAtTopOfView = !(boolean) XposedHelpers.callMethod(param.thisObject, "canScrollVertically", -1)
, 1000);
getHandler().postDelayed(() -> isAtTopOfView = true, 1000);
}
});
}
});

XposedHelpers.findAndHookMethod("android.support.v7.widget.RecyclerView", cl, "stopNestedScroll",
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
getHandler().removeCallbacksAndMessages(null);
isAtTopOfView = false;
getHandler().postDelayed(
() -> isAtTopOfView = !(boolean) XposedHelpers.callMethod(param.thisObject, "canScrollVertically", -1)
, 1000);
}
});
}

return bruteForceAds(cl);
}

private boolean bruteForceAds(ClassLoader cl) {
Instant start = Instant.now();
boolean foundBGClass = false, foundInVideoAds = false, foundCardAds = false, skip;
boolean foundBGClass = !Xposed.prefs.getBoolean("enable_bg_playback", true),
foundInVideoAds = false,
foundCardAds = !Xposed.prefs.getBoolean("hide_ad_cards", false),
skip;

Class3C heapPermutation = new Class3C();
while (heapPermutation.hasNext()) {
Expand Down Expand Up @@ -334,4 +323,4 @@ protected void onPostExecute(Boolean found) {
}
}

}
}
64 changes: 0 additions & 64 deletions app/src/main/java/ma/wanam/youtubeadaway/MainActivity.java

This file was deleted.

63 changes: 63 additions & 0 deletions app/src/main/java/ma/wanam/youtubeadaway/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ma.wanam.youtubeadaway;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {
private static SharedPreferences prefs;

@SuppressLint("WorldReadableFiles")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
try {
prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_WORLD_READABLE);
} catch (SecurityException ignored) {
Log.e(getPackageName(), ignored.toString());
}
}

public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);

// copy btc wallet address to the clipboard
getPreferenceScreen().findPreference("donate_btc")
.setOnPreferenceClickListener(preference -> {
ClipboardManager clipboardManager = (ClipboardManager)
getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText(preference.getKey(), preference.getSummary());
clipboardManager.setPrimaryClip(clipData);

return true;
});

// set module status
Preference statusPreference = getPreferenceScreen().findPreference("status");
statusPreference.setIcon(XChecker.isEnabled() ? android.R.drawable.ic_dialog_info : android.R.drawable.ic_dialog_alert);
statusPreference.setSummary(XChecker.isEnabled() ? R.string.module_active : R.string.module_inactive);
}
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/ma/wanam/youtubeadaway/Xposed.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package ma.wanam.youtubeadaway;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.IXposedHookZygoteInit;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
import ma.wanam.youtubeadaway.utils.Constants;
import ma.wanam.youtubeadaway.utils.Utils;

public class Xposed implements IXposedHookLoadPackage {
public class Xposed implements IXposedHookLoadPackage, IXposedHookZygoteInit {
public static XSharedPreferences prefs;

@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
Expand All @@ -24,6 +27,11 @@ public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
String ytVersion = Utils.getPackageVersion(lpparam);
XposedBridge.log("Hooking YouTube version: " + lpparam.packageName + " " + ytVersion);

if (prefs != null) {
prefs.reload();
} else
XposedBridge.log("CANNOT read module preferences!!!");

new BFAsync().execute(lpparam);
} catch (Throwable t) {
XposedBridge.log(t);
Expand All @@ -40,4 +48,9 @@ public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
}
}
}

@Override
public void initZygote(StartupParam startupParam) throws Throwable {
prefs = new XSharedPreferences(BuildConfig.APPLICATION_ID);
}
}
71 changes: 0 additions & 71 deletions app/src/main/res/layout/activity_main.xml

This file was deleted.

Loading

0 comments on commit 0b9698d

Please sign in to comment.