fix(youtube/settings): fix non functional back button in settings (#404)

This commit is contained in:
LisoUseInAIKyrios 2023-05-20 00:42:02 +04:00 committed by GitHub
parent d63f24f7be
commit 0c55d70370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 79 deletions

View File

@ -1,17 +1,18 @@
package app.revanced.integrations.settingsmenu;
import static app.revanced.integrations.utils.ReVancedUtils.getChildView;
import static app.revanced.integrations.utils.ReVancedUtils.getResourceIdentifier;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.preference.PreferenceFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.google.android.libraries.social.licenses.LicenseActivity;
import java.util.Objects;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.ThemeHelper;
public class ReVancedSettingActivity {
@ -19,76 +20,68 @@ public class ReVancedSettingActivity {
/**
* Injection point.
*/
public static void setTheme(LicenseActivity base) {
final var whiteTheme = "Theme.YouTube.Settings";
final var darkTheme = "Theme.YouTube.Settings.Dark";
final var theme = ThemeHelper.isDarkTheme() ? darkTheme : whiteTheme;
LogHelper.printDebug(() -> "Using theme: " + theme);
base.setTheme(ReVancedUtils.getResourceIdentifier(theme, "style"));
}
/**
* Injection point.
*/
public static void initializeSettings(LicenseActivity base) {
base.setContentView(ReVancedUtils.getResourceIdentifier("revanced_settings_with_toolbar", "layout"));
PreferenceFragment preferenceFragment;
String preferenceIdentifier;
String dataString = base.getIntent().getDataString();
if (dataString.equalsIgnoreCase("sponsorblock_settings")) {
preferenceIdentifier = "sb_settings";
preferenceFragment = new SponsorBlockSettingsFragment();
} else if (dataString.equalsIgnoreCase("ryd_settings")) {
preferenceIdentifier = "revanced_ryd_settings_title";
preferenceFragment = new ReturnYouTubeDislikeSettingsFragment();
} else {
preferenceIdentifier = "revanced_settings";
preferenceFragment = new ReVancedSettingsFragment();
}
public static void initializeSettings(Activity licenseActivity) {
try {
TextView toolbar = getTextView((ViewGroup) base.findViewById(ReVancedUtils.getResourceIdentifier("toolbar", "id")));
if (toolbar == null) {
// FIXME
// https://github.com/revanced/revanced-patches/issues/1384
LogHelper.printDebug(() -> "Could not find toolbar");
} else {
toolbar.setText(preferenceIdentifier);
ThemeHelper.setActivityTheme(licenseActivity);
licenseActivity.setContentView(
getResourceIdentifier("revanced_settings_with_toolbar", "layout"));
setBackButton(licenseActivity);
PreferenceFragment fragment;
String toolbarTitleResourceName;
String dataString = licenseActivity.getIntent().getDataString();
switch (dataString) {
case "sponsorblock_settings":
toolbarTitleResourceName = "revanced_sponsorblock_settings_title";
fragment = new SponsorBlockSettingsFragment();
break;
case "ryd_settings":
toolbarTitleResourceName = "revanced_ryd_settings_title";
fragment = new ReturnYouTubeDislikeSettingsFragment();
break;
case "revanced_settings":
toolbarTitleResourceName = "revanced_settings_title";
fragment = new ReVancedSettingsFragment();
break;
default:
LogHelper.printException(() -> "Unknown setting: " + dataString);
return;
}
} catch (Exception e) {
LogHelper.printException(() -> "Could not set Toolbar title", e);
setToolbarTitle(licenseActivity, toolbarTitleResourceName);
licenseActivity.getFragmentManager()
.beginTransaction()
.replace(getResourceIdentifier("revanced_settings_fragments", "id"), fragment)
.commit();
} catch (Exception ex) {
LogHelper.printException(() -> "onCreate failure", ex);
}
base.getFragmentManager().beginTransaction().replace(ReVancedUtils.getResourceIdentifier("revanced_settings_fragments", "id"), preferenceFragment).commit();
}
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"));
}
@Nullable
public static <T extends View> T getView(Class<T> typeClass, ViewGroup viewGroup) {
if (viewGroup == null) {
return null;
@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(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");
}
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = viewGroup.getChildAt(i);
if (childAt.getClass() == typeClass) {
return (T) childAt;
}
}
return null;
return toolbarResourceId;
}
@Nullable
public static ImageButton getImageButton(ViewGroup viewGroup) {
return getView(ImageButton.class, viewGroup);
}
@Nullable
public static TextView getTextView(ViewGroup viewGroup) {
return getView(TextView.class, viewGroup);
}
}

View File

@ -10,15 +10,25 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.revanced.integrations.settings.SettingsEnum;
import java.text.Bidi;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import app.revanced.integrations.settings.SettingsEnum;
public class ReVancedUtils {
@ -124,6 +134,24 @@ public class ReVancedUtils {
return getContext().getResources().getDimension(getResourceIdentifier(resourceIdentifierName, "dimen"));
}
/**
* @return The first child view that matches the filter.
*/
@Nullable
public static <T extends View> T getChildView(@NonNull ViewGroup viewGroup, @NonNull MatchFilter filter) {
for (int i = 0, childCount = viewGroup.getChildCount(); i < childCount; i++) {
View childAt = viewGroup.getChildAt(i);
if (filter.matches(childAt)) {
return (T) childAt;
}
}
return null;
}
public interface MatchFilter<T> {
boolean matches(T object);
}
public static Context getContext() {
if (context != null) {
return context;

View File

@ -1,5 +1,7 @@
package app.revanced.integrations.utils;
import android.app.Activity;
public class ThemeHelper {
private static int themeValue;
@ -7,7 +9,7 @@ public class ThemeHelper {
final int newOrdinalValue = ((Enum) value).ordinal();
if (themeValue != newOrdinalValue) {
themeValue = newOrdinalValue;
LogHelper.printDebug(() -> "Theme value: " + themeValue);
LogHelper.printDebug(() -> "Theme value: " + newOrdinalValue);
}
}
@ -15,4 +17,11 @@ public class ThemeHelper {
return themeValue == 1;
}
public static void setActivityTheme(Activity activity) {
final var theme = isDarkTheme()
? "Theme.YouTube.Settings.Dark"
: "Theme.YouTube.Settings";
activity.setTheme(ReVancedUtils.getResourceIdentifier(theme, "style"));
}
}

View File

@ -1,7 +0,0 @@
package com.google.android.libraries.social.licenses;
import android.app.Activity;
// Dummy class
public final class LicenseActivity extends Activity { }