diff --git a/extensions/shared/src/main/java/app/revanced/extension/shared/settings/preference/AbstractPreferenceFragment.java b/extensions/shared/src/main/java/app/revanced/extension/shared/settings/preference/AbstractPreferenceFragment.java index b7be999ff8..038dfefafc 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/shared/settings/preference/AbstractPreferenceFragment.java +++ b/extensions/shared/src/main/java/app/revanced/extension/shared/settings/preference/AbstractPreferenceFragment.java @@ -1,21 +1,33 @@ package app.revanced.extension.shared.settings.preference; +import static app.revanced.extension.shared.StringRef.str; +import static app.revanced.extension.shared.Utils.getResourceIdentifier; + import android.annotation.SuppressLint; import android.app.AlertDialog; +import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; +import android.graphics.drawable.Drawable; +import android.os.Build; import android.os.Bundle; import android.preference.*; +import android.util.TypedValue; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toolbar; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.util.Objects; + import app.revanced.extension.shared.Logger; import app.revanced.extension.shared.Utils; import app.revanced.extension.shared.settings.BaseSettings; import app.revanced.extension.shared.settings.BooleanSetting; import app.revanced.extension.shared.settings.Setting; - -import static app.revanced.extension.shared.StringRef.str; +import app.revanced.extension.youtube.ThemeHelper; @SuppressWarnings("deprecation") public abstract class AbstractPreferenceFragment extends PreferenceFragment { @@ -71,6 +83,15 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragment { } }; + @SuppressLint("UseCompatLoadingForDrawables") + public static Drawable getBackButtonDrawable() { + final int backButtonResource = getResourceIdentifier(ThemeHelper.isDarkTheme() + ? "yt_outline_arrow_left_white_24" + : "yt_outline_arrow_left_black_24", + "drawable"); + return Utils.getContext().getResources().getDrawable(backButtonResource); + } + /** * Initialize this instance, and do any custom behavior. *

@@ -98,7 +119,7 @@ private void showSettingUserDialogConfirmation(SwitchPreference switchPref, Bool showingUserDialogMessage = true; new AlertDialog.Builder(context) .setTitle(confirmDialogTitle) - .setMessage(setting.userDialogMessage.toString()) + .setMessage(Objects.requireNonNull(setting.userDialogMessage).toString()) .setPositiveButton(android.R.string.ok, (dialog, id) -> { if (setting.rebootApp) { showRestartDialog(context); @@ -261,6 +282,7 @@ public void onCreate(Bundle savedInstanceState) { // causes a callback to the listener even though nothing changed. initialize(); updateUIToSettingValues(); + setPreferenceScreenToolbar(getPreferenceScreen()); preferenceManager.getSharedPreferences().registerOnSharedPreferenceChangeListener(listener); } catch (Exception ex) { @@ -273,4 +295,44 @@ public void onDestroy() { getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener); super.onDestroy(); } + + private void setPreferenceScreenToolbar(PreferenceScreen parentScreen) { + for (int i = 0, preferenceCount = parentScreen.getPreferenceCount(); i < preferenceCount; i++) { + Preference childPreference = parentScreen.getPreference(i); + if (childPreference instanceof PreferenceScreen) { + // Recursively set sub preferences. + setPreferenceScreenToolbar((PreferenceScreen) childPreference); + + childPreference.setOnPreferenceClickListener( + childScreen -> { + Dialog preferenceScreenDialog = ((PreferenceScreen) childScreen).getDialog(); + ViewGroup rootView = (ViewGroup) preferenceScreenDialog + .findViewById(android.R.id.content) + .getParent(); + + Toolbar toolbar = new Toolbar(childScreen.getContext()); + toolbar.setTitle(childScreen.getTitle()); + toolbar.setNavigationIcon(getBackButtonDrawable()); + toolbar.setNavigationOnClickListener(view -> preferenceScreenDialog.dismiss()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + final int margin = (int) TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics() + ); + toolbar.setTitleMargin(margin, 0, margin, 0); + } + + TextView toolbarTextView = Utils.getChildView(toolbar, + true, TextView.class::isInstance); + if (toolbarTextView != null) { + toolbarTextView.setTextColor(ThemeHelper.getForegroundColor()); + } + + rootView.addView(toolbar, 0); + return false; + } + ); + } + } + } } diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/ThemeHelper.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/ThemeHelper.java index 6e0ea59742..9606587ddb 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/ThemeHelper.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/ThemeHelper.java @@ -82,4 +82,12 @@ private static int getColorInt(String colorString) { } return Utils.getResourceColor(colorString); } + + public static int getBackgroundColor() { + return isDarkTheme() ? getDarkThemeColor() : getLightThemeColor(); + } + + public static int getForegroundColor() { + return isDarkTheme() ? getLightThemeColor() : getDarkThemeColor(); + } } diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java index acf5657124..51409e2e42 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java @@ -7,6 +7,7 @@ import android.widget.ImageButton; import android.widget.TextView; import app.revanced.extension.shared.Logger; +import app.revanced.extension.shared.settings.preference.AbstractPreferenceFragment; import app.revanced.extension.youtube.ThemeHelper; import app.revanced.extension.youtube.settings.preference.ReVancedPreferenceFragment; import app.revanced.extension.youtube.settings.preference.ReturnYouTubeDislikePreferenceFragment; @@ -39,7 +40,7 @@ public static void initialize(Activity licenseActivity) { PreferenceFragment fragment; String toolbarTitleResourceName; - String dataString = licenseActivity.getIntent().getDataString(); + String dataString = Objects.requireNonNull(licenseActivity.getIntent().getDataString()); switch (dataString) { case "revanced_sb_settings_intent": toolbarTitleResourceName = "revanced_sb_settings_title"; @@ -59,12 +60,14 @@ public static void initialize(Activity licenseActivity) { } setToolbarTitle(licenseActivity, toolbarTitleResourceName); + + //noinspection deprecation licenseActivity.getFragmentManager() .beginTransaction() .replace(getResourceIdentifier("revanced_settings_fragments", "id"), fragment) .commit(); } catch (Exception ex) { - Logger.printException(() -> "onCreate failure", ex); + Logger.printException(() -> "initialize failure", ex); } } @@ -80,11 +83,7 @@ private static void setBackButton(Activity activity) { ViewGroup toolbar = activity.findViewById(getToolbarResourceId()); ImageButton imageButton = Objects.requireNonNull(getChildView(toolbar, false, view -> view instanceof ImageButton)); - final int backButtonResource = getResourceIdentifier(ThemeHelper.isDarkTheme() - ? "yt_outline_arrow_left_white_24" - : "yt_outline_arrow_left_black_24", - "drawable"); - imageButton.setImageDrawable(activity.getResources().getDrawable(backButtonResource)); + imageButton.setImageDrawable(AbstractPreferenceFragment.getBackButtonDrawable()); imageButton.setOnClickListener(view -> activity.onBackPressed()); }