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

Commit

Permalink
feat(YouTube): Add 'About' preference to settings menu (#608)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <[email protected]>
  • Loading branch information
LisoUseInAIKyrios and oSumAtrIX authored Apr 17, 2024
1 parent f5720f7 commit b8f260e
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 92 deletions.
85 changes: 53 additions & 32 deletions app/src/main/java/app/revanced/integrations/shared/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.concurrent.TimeUnit;

import app.revanced.integrations.shared.settings.BooleanSetting;
import app.revanced.integrations.shared.settings.preference.ReVancedAboutPreference;
import kotlin.text.Regex;

public class Utils {
Expand All @@ -47,30 +48,44 @@ public class Utils {
private Utils() {
} // utility class

public static String getVersionName() {
if (versionName != null) return versionName;

PackageInfo packageInfo;
try {
final var packageName = Objects.requireNonNull(getContext()).getPackageName();

PackageManager packageManager = context.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
packageInfo = packageManager.getPackageInfo(
packageName,
PackageManager.PackageInfoFlags.of(0)
);
else
packageInfo = packageManager.getPackageInfo(
packageName,
0
);
} catch (PackageManager.NameNotFoundException e) {
Logger.printException(() -> "Failed to get package info", e);
return null;
/**
* Injection point.
*
* @return The manifest 'Version' entry of the patches.jar used during patching.
*/
public static String getPatchesReleaseVersion() {
return ""; // Value is replaced during patching.
}

/**
* @return The version name of the app, such as "YouTube".
*/
public static String getAppVersionName() {
if (versionName == null) {
try {
final var packageName = Objects.requireNonNull(getContext()).getPackageName();

PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageInfo = packageManager.getPackageInfo(
packageName,
PackageManager.PackageInfoFlags.of(0)
);
} else {
packageInfo = packageManager.getPackageInfo(
packageName,
0
);
}
versionName = packageInfo.versionName;
} catch (Exception ex) {
Logger.printException(() -> "Failed to get package info", ex);
versionName = "Unknown";
}
}

return versionName = packageInfo.versionName;
return versionName;
}

/**
Expand Down Expand Up @@ -185,6 +200,7 @@ public static Animation getResourceAnimation(@NonNull String resourceIdentifierN
}

public static int getResourceColor(@NonNull String resourceIdentifierName) throws Resources.NotFoundException {
//noinspection deprecation
return getContext().getResources().getColor(getResourceIdentifier(resourceIdentifierName, "color"));
}

Expand Down Expand Up @@ -323,7 +339,7 @@ public static void runOnMainThreadDelayed(@NonNull Runnable runnable, long delay
try {
runnable.run();
} catch (Exception ex) {
Logger.printException(() -> runnable.getClass() + ": " + ex.getMessage(), ex);
Logger.printException(() -> runnable.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
}
};
new Handler(Looper.getMainLooper()).postDelayed(loggingRunnable, delayMillis);
Expand Down Expand Up @@ -445,19 +461,16 @@ private enum Sort {
this.keySuffix = keySuffix;
}

/**
* Defaults to {@link #UNSORTED} if key is null or has no sort suffix.
*/
@NonNull
static Sort fromKey(@Nullable String key) {
static Sort fromKey(@Nullable String key, @NonNull Sort defaultSort) {
if (key != null) {
for (Sort sort : values()) {
if (key.endsWith(sort.keySuffix)) {
return sort;
}
}
}
return UNSORTED;
return defaultSort;
}
}

Expand All @@ -479,19 +492,26 @@ public static String removePunctuationConvertToLowercase(@Nullable CharSequence
* If a preference has no key or no {@link Sort} suffix,
* then the preferences are left unsorted.
*/
@SuppressWarnings("deprecation")
public static void sortPreferenceGroups(@NonNull PreferenceGroup group) {
Sort sort = Sort.fromKey(group.getKey());
Sort groupSort = Sort.fromKey(group.getKey(), Sort.UNSORTED);
SortedMap<String, Preference> preferences = new TreeMap<>();

for (int i = 0, prefCount = group.getPreferenceCount(); i < prefCount; i++) {
Preference preference = group.getPreference(i);

final Sort preferenceSort;
if (preference instanceof PreferenceGroup) {
sortPreferenceGroups((PreferenceGroup) preference);
preferenceSort = groupSort; // Sort value for groups is for it's content, not itself.
} else {
// Allow individual preferences to set a key sorting.
// Used to force a preference to the top or bottom of a group.
preferenceSort = Sort.fromKey(preference.getKey(), groupSort);
}

final String sortValue;
switch (sort) {
switch (preferenceSort) {
case BY_TITLE:
sortValue = removePunctuationConvertToLowercase(preference.getTitle());
break;
Expand All @@ -511,8 +531,9 @@ public static void sortPreferenceGroups(@NonNull PreferenceGroup group) {
for (Preference pref : preferences.values()) {
int order = index++;

// If the preference is a PreferenceScreen or is an intent preference, move to the top.
if (pref instanceof PreferenceScreen || pref.getIntent() != null) {
// Move any screens, intents, and the one off About preference to the top.
if (pref instanceof PreferenceScreen || pref instanceof ReVancedAboutPreference
|| pref.getIntent() != null) {
// Arbitrary high number.
order -= 1000;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

import static app.revanced.integrations.shared.StringRef.str;

/**
*
*
* @noinspection deprecation, DataFlowIssue , unused */
@SuppressWarnings({"unused", "deprecation"})
public abstract class AbstractPreferenceFragment extends PreferenceFragment {
/**
* Indicates that if a preference changes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static app.revanced.integrations.shared.StringRef.str;

/** @noinspection deprecation, unused */
@SuppressWarnings({"unused", "deprecation"})
public class ImportExportPreference extends EditTextPreference implements Preference.OnPreferenceClickListener {

private String existingSettings;
Expand Down
Loading

0 comments on commit b8f260e

Please sign in to comment.