chore: Lint code

This commit is contained in:
inotia00 2025-03-06 11:48:35 +09:00
parent 02d7bb71cd
commit 4cd5cc854b
9 changed files with 69 additions and 71 deletions

View File

@ -23,16 +23,13 @@ import app.revanced.extension.shared.settings.StringSetting;
import app.revanced.extension.shared.utils.ResourceUtils; import app.revanced.extension.shared.utils.ResourceUtils;
import app.revanced.extension.shared.utils.Utils; import app.revanced.extension.shared.utils.Utils;
/** @SuppressWarnings("StringOperationCanBeSimplified")
* @noinspection all
*/
public class ExternalDownloaderPreference { public class ExternalDownloaderPreference {
private static final StringSetting settings = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME; private static final StringSetting settings = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME;
private static final String[] mEntries = ResourceUtils.getStringArray("revanced_external_downloader_label"); private static final String[] mEntries = ResourceUtils.getStringArray("revanced_external_downloader_label");
private static final String[] mEntryValues = ResourceUtils.getStringArray("revanced_external_downloader_package_name"); private static final String[] mEntryValues = ResourceUtils.getStringArray("revanced_external_downloader_package_name");
private static final String[] mWebsiteEntries = ResourceUtils.getStringArray("revanced_external_downloader_website"); private static final String[] mWebsiteEntries = ResourceUtils.getStringArray("revanced_external_downloader_website");
private static EditText mEditText;
private static String packageName; private static String packageName;
private static int mClickedDialogEntryIndex; private static int mClickedDialogEntryIndex;
@ -50,7 +47,7 @@ public class ExternalDownloaderPreference {
}; };
public static void showDialog(Activity mActivity) { public static void showDialog(Activity mActivity) {
packageName = settings.get().toString(); packageName = settings.get();
mClickedDialogEntryIndex = Arrays.asList(mEntryValues).indexOf(packageName); mClickedDialogEntryIndex = Arrays.asList(mEntryValues).indexOf(packageName);
AlertDialog.Builder builder = getDialogBuilder(mActivity); AlertDialog.Builder builder = getDialogBuilder(mActivity);
@ -61,7 +58,7 @@ public class ExternalDownloaderPreference {
TableRow row = new TableRow(mActivity); TableRow row = new TableRow(mActivity);
mEditText = new EditText(mActivity); EditText mEditText = new EditText(mActivity);
mEditText.setText(packageName); mEditText.setText(packageName);
mEditText.addTextChangedListener(textWatcher); mEditText.addTextChangedListener(textWatcher);
mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PT, 9); mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PT, 9);
@ -94,10 +91,8 @@ public class ExternalDownloaderPreference {
if (mClickedDialogEntryIndex >= 0) { if (mClickedDialogEntryIndex >= 0) {
appName = mEntries[mClickedDialogEntryIndex].toString(); appName = mEntries[mClickedDialogEntryIndex].toString();
website = mWebsiteEntries[mClickedDialogEntryIndex].toString(); website = mWebsiteEntries[mClickedDialogEntryIndex].toString();
return showToastOrOpenWebsites(mActivity, appName, packageName, website);
} else {
return showToastOrOpenWebsites(mActivity, appName, packageName, website);
} }
return showToastOrOpenWebsites(mActivity, appName, packageName, website);
} }
private static boolean showToastOrOpenWebsites(Activity mActivity, String appName, String packageName, String website) { private static boolean showToastOrOpenWebsites(Activity mActivity, String appName, String packageName, String website) {

View File

@ -53,7 +53,8 @@ public class VideoUtils extends IntentUtils {
return; return;
} }
if (context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE) instanceof AudioManager audioManager) { Context mContext = getContext();
if (mContext != null && mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE) instanceof AudioManager audioManager) {
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
} }
@ -68,7 +69,10 @@ public class VideoUtils extends IntentUtils {
public static void openInYouTubeMusic(@NonNull String songId) { public static void openInYouTubeMusic(@NonNull String songId) {
final String url = String.format("vnd.youtube.music://%s", songId); final String url = String.format("vnd.youtube.music://%s", songId);
launchView(url, context.getPackageName()); Context mContext = getContext();
if (mContext != null) {
launchView(url, mContext.getPackageName());
}
} }
/** /**

View File

@ -1,5 +1,7 @@
package app.revanced.extension.shared.patches.components; package app.revanced.extension.shared.patches.components;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@ -12,6 +14,7 @@ import java.util.function.Consumer;
import app.revanced.extension.shared.utils.TrieSearch; import app.revanced.extension.shared.utils.TrieSearch;
@SuppressLint("ObsoleteSdkInt")
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class FilterGroupList<V, T extends FilterGroup<V>> implements Iterable<T> { public abstract class FilterGroupList<V, T extends FilterGroup<V>> implements Iterable<T> {

View File

@ -23,7 +23,8 @@ public class IntentUtils extends Utils {
Context mContext = getActivity(); Context mContext = getActivity();
if (mContext == null) { if (mContext == null) {
// Utils context is the application context, and not an activity context. // Utils context is the application context, and not an activity context.
mContext = context; mContext = getContext();
if (mContext == null) return;
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} }
mContext.startActivity(intent); mContext.startActivity(intent);

View File

@ -31,7 +31,7 @@ public class PackageUtils extends Utils {
public static boolean isPackageEnabled(@NonNull String packageName) { public static boolean isPackageEnabled(@NonNull String packageName) {
try { try {
return context.getPackageManager().getApplicationInfo(packageName, 0).enabled; return getContext().getPackageManager().getApplicationInfo(packageName, 0).enabled;
} catch (PackageManager.NameNotFoundException ignored) { } catch (PackageManager.NameNotFoundException ignored) {
} }
@ -43,7 +43,7 @@ public class PackageUtils extends Utils {
} }
public static int getSmallestScreenWidthDp() { public static int getSmallestScreenWidthDp() {
return context.getResources().getConfiguration().smallestScreenWidthDp; return getContext().getResources().getConfiguration().smallestScreenWidthDp;
} }
// utils // utils
@ -51,7 +51,7 @@ public class PackageUtils extends Utils {
private static PackageInfo getPackageInfo() { private static PackageInfo getPackageInfo() {
try { try {
final PackageManager packageManager = getPackageManager(); final PackageManager packageManager = getPackageManager();
final String packageName = context.getPackageName(); final String packageName = getContext().getPackageName();
return isSDKAbove(33) return isSDKAbove(33)
? packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0)) ? packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
: packageManager.getPackageInfo(packageName, 0); : packageManager.getPackageInfo(packageName, 0);
@ -63,7 +63,7 @@ public class PackageUtils extends Utils {
@NonNull @NonNull
private static PackageManager getPackageManager() { private static PackageManager getPackageManager() {
return context.getPackageManager(); return getContext().getPackageManager();
} }
public static boolean isVersionToLessThan(@NonNull String compareVersion, @NonNull String targetVersion) { public static boolean isVersionToLessThan(@NonNull String compareVersion, @NonNull String targetVersion) {

View File

@ -8,9 +8,7 @@ import android.view.animation.AnimationUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
/** @SuppressWarnings({"unused", "deprecation", "DiscouragedApi"})
* @noinspection ALL
*/
public class ResourceUtils extends Utils { public class ResourceUtils extends Utils {
private ResourceUtils() { private ResourceUtils() {

View File

@ -53,9 +53,7 @@ import kotlin.text.Regex;
public class Utils { public class Utils {
private static WeakReference<Activity> activityRef = new WeakReference<>(null); private static WeakReference<Activity> activityRef = new WeakReference<>(null);
private static WeakReference<Context> contextRef = new WeakReference<>(null);
@SuppressLint("StaticFieldLeak")
public static Context context;
protected Utils() { protected Utils() {
} // utility class } // utility class
@ -276,15 +274,17 @@ public class Utils {
} }
public static Context getContext() { public static Context getContext() {
if (context == null) { Context mContext = contextRef.get();
if (mContext == null) {
Logger.initializationException(Utils.class, "Context is null, returning null!", null); Logger.initializationException(Utils.class, "Context is null, returning null!", null);
} }
return context; return mContext;
} }
public static Resources getResources() { public static Resources getResources() {
if (context != null) { Context mContext = contextRef.get();
return context.getResources(); if (mContext != null) {
return mContext.getResources();
} }
Activity mActivity = activityRef.get(); Activity mActivity = activityRef.get();
if (mActivity != null) { if (mActivity != null) {
@ -346,36 +346,17 @@ public class Utils {
return; return;
} }
context = appContext; // Must initially set context to check the app language.
contextRef = new WeakReference<>(appContext);
Logger.initializationInfo(Utils.class, "Set context: " + appContext);
AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get(); AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get();
if (language != AppLanguage.DEFAULT) { if (language != AppLanguage.DEFAULT) {
// Create a new context with the desired language. // Create a new context with the desired language.
Configuration config = appContext.getResources().getConfiguration(); Configuration config = appContext.getResources().getConfiguration();
config.setLocale(language.getLocale()); config.setLocale(language.getLocale());
context = appContext.createConfigurationContext(config); contextRef = new WeakReference<>(appContext.createConfigurationContext(config));
} }
// In some apps like TikTok, the Setting classes can load in weird orders due to cyclic class dependencies.
// Calling the regular printDebug method here can cause a Settings context null pointer exception,
// even though the context is already set before the call.
//
// The initialization logger methods do not directly or indirectly
// reference the Context or any Settings and are unaffected by this problem.
//
// Info level also helps debug if a patch hook is called before
// the context is set since debug logging is off by default.
StringBuilder sb = new StringBuilder();
sb.append("Set context: ");
sb.append(appContext);
StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
if (stackTraceElement.length > 3) {
sb.append("\n");
sb.append("Called from method: ");
sb.append(stackTraceElement[3]);
}
Logger.initializationInfo(Utils.class, sb.toString());
} }
public static void setClipboard(@NonNull String text) { public static void setClipboard(@NonNull String text) {
@ -383,10 +364,10 @@ public class Utils {
} }
public static void setClipboard(@NonNull String text, @Nullable String toastMessage) { public static void setClipboard(@NonNull String text, @Nullable String toastMessage) {
if (!(context.getSystemService(Context.CLIPBOARD_SERVICE) instanceof ClipboardManager clipboard)) Context mContext = contextRef.get();
return; if (mContext != null && mContext.getSystemService(Context.CLIPBOARD_SERVICE) instanceof ClipboardManager clipboardManager) {
android.content.ClipData clip = android.content.ClipData.newPlainText("ReVanced", text); android.content.ClipData clip = android.content.ClipData.newPlainText("ReVanced", text);
clipboard.setPrimaryClip(clip); clipboardManager.setPrimaryClip(clip);
// Do not show a toast if using Android 13+ as it shows it's own toast. // Do not show a toast if using Android 13+ as it shows it's own toast.
// But if the user copied with a timestamp then show a toast. // But if the user copied with a timestamp then show a toast.
@ -394,6 +375,7 @@ public class Utils {
if (isSDKAbove(33) || toastMessage == null) return; if (isSDKAbove(33) || toastMessage == null) return;
showToastShort(toastMessage); showToastShort(toastMessage);
} }
}
public static String getFormattedTimeStamp(long videoTime) { public static String getFormattedTimeStamp(long videoTime) {
return "'" + videoTime + return "'" + videoTime +
@ -534,11 +516,21 @@ public class Utils {
} }
public static int dpToPx(float dp) { public static int dpToPx(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); Context mContext = contextRef.get();
if (mContext == null) {
return (int) dp;
} else {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mContext.getResources().getDisplayMetrics());
}
} }
public static int dpToPx(int dp) { public static int dpToPx(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); Context mContext = contextRef.get();
if (mContext == null) {
return dp;
} else {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mContext.getResources().getDisplayMetrics());
}
} }
/** /**
@ -558,18 +550,20 @@ public class Utils {
private static void showToast(@NonNull String messageToToast, int toastDuration) { private static void showToast(@NonNull String messageToToast, int toastDuration) {
Objects.requireNonNull(messageToToast); Objects.requireNonNull(messageToToast);
runOnMainThreadNowOrLater(() -> { runOnMainThreadNowOrLater(() -> {
if (context == null) { Context mContext = contextRef.get();
if (mContext == null) {
Logger.initializationException(Utils.class, "Cannot show toast (context is null): " + messageToToast, null); Logger.initializationException(Utils.class, "Cannot show toast (context is null): " + messageToToast, null);
} else { } else {
Logger.printDebug(() -> "Showing toast: " + messageToToast); Logger.printDebug(() -> "Showing toast: " + messageToToast);
Toast.makeText(context, messageToToast, toastDuration).show(); Toast.makeText(mContext, messageToToast, toastDuration).show();
} }
} });
);
} }
public static boolean isLandscapeOrientation() { public static boolean isLandscapeOrientation() {
final int orientation = context.getResources().getConfiguration().orientation; Context mContext = contextRef.get();
if (mContext == null) return false;
final int orientation = mContext.getResources().getConfiguration().orientation;
return orientation == Configuration.ORIENTATION_LANDSCAPE; return orientation == Configuration.ORIENTATION_LANDSCAPE;
} }
@ -660,7 +654,8 @@ public class Utils {
@SuppressLint("MissingPermission") // permission already included in YouTube @SuppressLint("MissingPermission") // permission already included in YouTube
public static NetworkType getNetworkType() { public static NetworkType getNetworkType() {
if (context == null || !(context.getSystemService(Context.CONNECTIVITY_SERVICE) instanceof ConnectivityManager cm)) Context mContext = contextRef.get();
if (mContext == null || !(mContext.getSystemService(Context.CONNECTIVITY_SERVICE) instanceof ConnectivityManager cm))
return NetworkType.NONE; return NetworkType.NONE;
final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); final NetworkInfo networkInfo = cm.getActiveNetworkInfo();

View File

@ -55,6 +55,7 @@ public final class AdsFilter extends Filter {
// "video_display_button_group_layout" // "video_display_button_group_layout"
// "video_display_carousel_button_group_layout" // "video_display_carousel_button_group_layout"
// "video_display_full_buttoned_layout" // "video_display_full_buttoned_layout"
// "video_display_full_buttoned_short_dr_layout"
// "video_display_full_layout" // "video_display_full_layout"
// "video_display_full_layout.eml" // "video_display_full_layout.eml"
"video_display_", "video_display_",

View File

@ -170,7 +170,8 @@ public class VideoUtils extends IntentUtils {
* Pause the media by changing audio focus. * Pause the media by changing audio focus.
*/ */
public static void pauseMedia() { public static void pauseMedia() {
if (context != null && context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE) instanceof AudioManager audioManager) { Context mContext = getContext();
if (mContext != null && mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE) instanceof AudioManager audioManager) {
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
} }
} }