diff --git a/app/src/main/java/app/revanced/integrations/music/settings/FullStackTraceActivityHook.java b/app/src/main/java/app/revanced/integrations/music/settings/FullStackTraceActivityHook.java new file mode 100644 index 00000000..38f4e1f5 --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/music/settings/FullStackTraceActivityHook.java @@ -0,0 +1,86 @@ +package app.revanced.integrations.music.settings; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.preference.PreferenceFragment; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.TextView; +import app.revanced.integrations.music.settings.preference.ReVancedPreferenceFragment; +import app.revanced.integrations.shared.Logger; + +import java.util.Objects; + +import static app.revanced.integrations.shared.Utils.getChildView; +import static app.revanced.integrations.shared.Utils.getResourceIdentifier; + +/** + * Hooks FullStackTraceActivityHook. + *

+ * This class is responsible for injecting our own fragment by replacing the FullStackTraceActivityHook. + */ +@SuppressWarnings("unused") +public class FullStackTraceActivityHook { + + /** + * Injection point. + *

+ * Hooks FullStackTraceActivityHook#onCreate in order to inject our own fragment. + */ + public static void initialize(Activity fullStackTraceActivityHook) { + try { + // ThemeHelper.setActivityTheme(fullStackTraceActivityHook); + fullStackTraceActivityHook.setContentView( + getResourceIdentifier("revanced_settings_with_toolbar", "layout")); + setBackButton(fullStackTraceActivityHook); + + PreferenceFragment fragment; + String toolbarTitleResourceName; + String dataString = fullStackTraceActivityHook.getIntent().getDataString(); + switch (dataString) { + case "revanced_settings_intent": + toolbarTitleResourceName = "revanced_settings_title"; + fragment = new ReVancedPreferenceFragment(); + break; + default: + Logger.printException(() -> "Unknown setting: " + dataString); + return; + } + + setToolbarTitle(fullStackTraceActivityHook, toolbarTitleResourceName); + fullStackTraceActivityHook.getFragmentManager() + .beginTransaction() + .replace(getResourceIdentifier("revanced_settings_fragments", "id"), fragment) + .commit(); + } catch (Exception ex) { + Logger.printException(() -> "onCreate failure", ex); + } + } + + private static void setToolbarTitle(Activity activity, String toolbarTitleResourceName) { + ViewGroup toolbar = activity.findViewById(getToolbarResourceId()); + TextView toolbarTextView = Objects.requireNonNull(getChildView(toolbar, view -> view instanceof TextView)); + toolbarTextView.setText(getResourceIdentifier(toolbarTitleResourceName, "string")); + } + + @SuppressLint("UseCompatLoadingForDrawables") + private static void setBackButton(Activity activity) { + ViewGroup toolbar = activity.findViewById(getToolbarResourceId()); + ImageButton imageButton = Objects.requireNonNull(getChildView(toolbar, view -> view instanceof ImageButton)); + final int backButtonResource = getResourceIdentifier(false // ThemeHelper.isDarkTheme() + ? "yt_outline_arrow_left_white_24" + : "yt_outline_arrow_left_black_24", + "drawable"); + imageButton.setImageDrawable(activity.getResources().getDrawable(backButtonResource)); + imageButton.setOnClickListener(view -> activity.onBackPressed()); + } + + private static int getToolbarResourceId() { + final int toolbarResourceId = getResourceIdentifier("revanced_toolbar", "id"); + if (toolbarResourceId == 0) { + throw new IllegalStateException("Could not find back button resource"); + } + return toolbarResourceId; + } + +} diff --git a/app/src/main/java/app/revanced/integrations/music/settings/Settings.java b/app/src/main/java/app/revanced/integrations/music/settings/Settings.java new file mode 100644 index 00000000..f3571417 --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/music/settings/Settings.java @@ -0,0 +1,7 @@ +package app.revanced.integrations.music.settings; + +import app.revanced.integrations.shared.settings.BaseSettings; + +public class Settings extends BaseSettings { + // TODO Add settings +} diff --git a/app/src/main/java/app/revanced/integrations/music/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/music/settings/preference/ReVancedPreferenceFragment.java new file mode 100644 index 00000000..6155231d --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/music/settings/preference/ReVancedPreferenceFragment.java @@ -0,0 +1,20 @@ +package app.revanced.integrations.music.settings.preference; + +import app.revanced.integrations.music.settings.Settings; +import app.revanced.integrations.shared.Logger; +import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; + +/** + * Preference fragment for ReVanced settings. + */ +public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { + @Override + protected void initialize() { + super.initialize(); + + // Do anything that forces this apps Settings bundle to load. + if (Settings.DEBUG.get()) { + Logger.printDebug(() -> "Debug logging enabled"); // Any statement that references the app settings. + } + } +} diff --git a/app/src/main/java/app/revanced/integrations/twitch/settings/AppCompatActivityHook.java b/app/src/main/java/app/revanced/integrations/twitch/settings/AppCompatActivityHook.java index f08fec13..14ab4244 100644 --- a/app/src/main/java/app/revanced/integrations/twitch/settings/AppCompatActivityHook.java +++ b/app/src/main/java/app/revanced/integrations/twitch/settings/AppCompatActivityHook.java @@ -45,7 +45,7 @@ public class AppCompatActivityHook { * @return Returns string resource id */ public static int getReVancedSettingsString() { - return app.revanced.integrations.twitch.Utils.getStringId("revanced_settings"); + return app.revanced.integrations.twitch.Utils.getStringId("revanced_settings_title"); } /** @@ -101,7 +101,7 @@ public class AppCompatActivityHook { ReVancedPreferenceFragment fragment = new ReVancedPreferenceFragment(); ActionBar supportActionBar = base.getSupportActionBar(); if (supportActionBar != null) - supportActionBar.setTitle(app.revanced.integrations.twitch.Utils.getStringId("revanced_settings")); + supportActionBar.setTitle(app.revanced.integrations.twitch.Utils.getStringId("revanced_settings_title")); base.getFragmentManager() .beginTransaction()