chore: Lint code

This commit is contained in:
inotia00
2025-02-10 16:34:48 +09:00
parent 9e1f6726a7
commit 1fb36685ad
6 changed files with 39 additions and 41 deletions

View File

@ -14,12 +14,14 @@ import app.revanced.extension.shared.utils.Logger;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class ReturnYouTubeUsernamePatch { public class ReturnYouTubeUsernamePatch {
private static final boolean RETURN_YOUTUBE_USERNAME_ENABLED = BaseSettings.RETURN_YOUTUBE_USERNAME_ENABLED.get(); private static final String YOUTUBE_API_KEY =
private static final Boolean RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT = BaseSettings.RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT.get().userNameFirst; BaseSettings.RETURN_YOUTUBE_USERNAME_YOUTUBE_DATA_API_V3_DEVELOPER_KEY.get();
private static final String YOUTUBE_API_KEY = BaseSettings.RETURN_YOUTUBE_USERNAME_YOUTUBE_DATA_API_V3_DEVELOPER_KEY.get(); private static final boolean RETURN_YOUTUBE_USERNAME_ENABLED =
BaseSettings.RETURN_YOUTUBE_USERNAME_ENABLED.get() && !YOUTUBE_API_KEY.isEmpty();
private static final Boolean RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT =
BaseSettings.RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT.get().userNameFirst;
private static final String AUTHOR_BADGE_PATH = "|author_badge.eml|"; private static final String AUTHOR_BADGE_PATH = "|author_badge.eml|";
private static volatile String lastFetchedHandle = "";
/** /**
* Injection point. * Injection point.
@ -51,18 +53,12 @@ public class ReturnYouTubeUsernamePatch {
if (!RETURN_YOUTUBE_USERNAME_ENABLED) { if (!RETURN_YOUTUBE_USERNAME_ENABLED) {
return original; return original;
} }
if (YOUTUBE_API_KEY.isEmpty()) {
Logger.printDebug(() -> "API key is empty");
return original;
}
// In comments, the path to YouTube Handle(@youtube) always includes [AUTHOR_BADGE_PATH]. // In comments, the path to YouTube Handle(@youtube) always includes [AUTHOR_BADGE_PATH].
if (!conversionContext.toString().contains(AUTHOR_BADGE_PATH)) { if (!conversionContext.toString().contains(AUTHOR_BADGE_PATH)) {
return original; return original;
} }
String handle = original.toString(); String handle = original.toString();
if (fetchNeeded && !handle.equals(lastFetchedHandle)) { if (fetchNeeded) {
lastFetchedHandle = handle;
// Get the original username using YouTube Data API v3.
ChannelRequest.fetchRequestIfNeeded(handle, YOUTUBE_API_KEY, RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT); ChannelRequest.fetchRequestIfNeeded(handle, YOUTUBE_API_KEY, RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT);
return original; return original;
} }
@ -77,9 +73,7 @@ public class ReturnYouTubeUsernamePatch {
Logger.printDebug(() -> "ChannelRequest Stream is null, handle:" + handle); Logger.printDebug(() -> "ChannelRequest Stream is null, handle:" + handle);
return original; return original;
} }
final CharSequence copiedSpannableString = copySpannableString(original, userName); return copySpannableString(original, userName);
Logger.printDebug(() -> "Replaced: '" + original + "' with: '" + copiedSpannableString + "'");
return copiedSpannableString;
} catch (Exception ex) { } catch (Exception ex) {
Logger.printException(() -> "onLithoTextLoaded failure", ex); Logger.printException(() -> "onLithoTextLoaded failure", ex);
} }

View File

@ -89,22 +89,21 @@ public class BlockRequestPatch {
* <p> * <p>
* Blocks /initplayback requests. * Blocks /initplayback requests.
*/ */
public static String blockInitPlaybackRequest(String originalUrlString) { public static Uri blockInitPlaybackRequest(Uri initPlaybackRequestUri) {
if (BLOCK_REQUEST) { if (BLOCK_REQUEST) {
try { try {
var originalUri = Uri.parse(originalUrlString); String path = initPlaybackRequestUri.getPath();
String path = originalUri.getPath();
if (path != null && path.contains("initplayback")) { if (path != null && path.contains("initplayback")) {
Logger.printDebug(() -> "Blocking 'initplayback' by clearing query"); Logger.printDebug(() -> "Blocking 'initplayback' by clearing query");
return originalUri.buildUpon().clearQuery().build().toString(); return initPlaybackRequestUri.buildUpon().clearQuery().build();
} }
} catch (Exception ex) { } catch (Exception ex) {
Logger.printException(() -> "blockInitPlaybackRequest failure", ex); Logger.printException(() -> "blockInitPlaybackRequest failure", ex);
} }
} }
return originalUrlString; return initPlaybackRequestUri;
} }
} }

View File

@ -50,14 +50,18 @@ public class ChannelRequest {
}); });
public static void fetchRequestIfNeeded(@NonNull String handle, @NonNull String apiKey, Boolean userNameFirst) { public static void fetchRequestIfNeeded(@NonNull String handle, @NonNull String apiKey, Boolean userNameFirst) {
if (!cache.containsKey(handle)) { synchronized (cache) {
cache.put(handle, new ChannelRequest(handle, apiKey, userNameFirst)); if (cache.get(handle) == null) {
cache.put(handle, new ChannelRequest(handle, apiKey, userNameFirst));
}
} }
} }
@Nullable @Nullable
public static ChannelRequest getRequestForHandle(@NonNull String handle) { public static ChannelRequest getRequestForHandle(@NonNull String handle) {
return cache.get(handle); synchronized (cache) {
return cache.get(handle);
}
} }
private static void handleConnectionError(String toastMessage, @Nullable Exception ex) { private static void handleConnectionError(String toastMessage, @Nullable Exception ex) {
@ -108,7 +112,8 @@ public class ChannelRequest {
final String userName = channelJsonObject final String userName = channelJsonObject
.getJSONArray("items") .getJSONArray("items")
.getJSONObject(0) .getJSONObject(0)
.getJSONObject("snippet") .getJSONObject("brandingSettings")
.getJSONObject("channel")
.getString("title"); .getString("title");
return authorBadgeBuilder(handle, userName, userNameFirst); return authorBadgeBuilder(handle, userName, userNameFirst);
} catch (JSONException e) { } catch (JSONException e) {

View File

@ -11,7 +11,7 @@ import app.revanced.extension.shared.requests.Route;
public class ChannelRoutes { public class ChannelRoutes {
public static final String YOUTUBEI_V3_GAPIS_URL = "https://www.googleapis.com/youtube/v3/"; public static final String YOUTUBEI_V3_GAPIS_URL = "https://www.googleapis.com/youtube/v3/";
public static final Route GET_CHANNEL_DETAILS = new Route(GET, "channels?part=snippet&forHandle={handle}&key={api_key}"); public static final Route GET_CHANNEL_DETAILS = new Route(GET, "channels?part=brandingSettings&maxResults=1&prettyPrint=false&forHandle={handle}&key={api_key}");
public ChannelRoutes() { public ChannelRoutes() {
} }

View File

@ -4,7 +4,6 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.bytecodePatch import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patches.shared.extension.Constants.SPOOF_PATH import app.revanced.patches.shared.extension.Constants.SPOOF_PATH
import app.revanced.util.fingerprint.matchOrThrow
import app.revanced.util.fingerprint.methodOrThrow import app.revanced.util.fingerprint.methodOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@ -18,20 +17,18 @@ val blockRequestPatch = bytecodePatch(
execute { execute {
// region Block /initplayback requests to fall back to /get_watch requests. // region Block /initplayback requests to fall back to /get_watch requests.
buildInitPlaybackRequestFingerprint.matchOrThrow().let { buildInitPlaybackRequestFingerprint.methodOrThrow().apply {
it.method.apply { val uriIndex = indexOfUriToStringInstruction(this)
val moveUriStringIndex = it.patternMatch!!.startIndex val uriRegister =
val targetRegister = getInstruction<FiveRegisterInstruction>(uriIndex).registerC
getInstruction<OneRegisterInstruction>(moveUriStringIndex).registerA
addInstructions( addInstructions(
moveUriStringIndex + 1, uriIndex,
""" """
invoke-static { v$targetRegister }, $EXTENSION_CLASS_DESCRIPTOR->blockInitPlaybackRequest(Ljava/lang/String;)Ljava/lang/String; invoke-static { v$uriRegister }, $EXTENSION_CLASS_DESCRIPTOR->blockInitPlaybackRequest(Landroid/net/Uri;)Landroid/net/Uri;
move-result-object v$targetRegister move-result-object v$uriRegister
""", """,
) )
}
} }
// endregion // endregion
@ -39,7 +36,7 @@ val blockRequestPatch = bytecodePatch(
// region Block /get_watch requests to fall back to /player requests. // region Block /get_watch requests to fall back to /player requests.
buildPlayerRequestURIFingerprint.methodOrThrow().apply { buildPlayerRequestURIFingerprint.methodOrThrow().apply {
val invokeToStringIndex = indexOfToStringInstruction(this) val invokeToStringIndex = indexOfUriToStringInstruction(this)
val uriRegister = val uriRegister =
getInstruction<FiveRegisterInstruction>(invokeToStringIndex).registerC getInstruction<FiveRegisterInstruction>(invokeToStringIndex).registerC

View File

@ -18,6 +18,9 @@ internal val buildInitPlaybackRequestFingerprint = legacyFingerprint(
"Content-Type", "Content-Type",
"Range", "Range",
), ),
customFingerprint = { method, _ ->
indexOfUriToStringInstruction(method) >= 0
},
) )
internal val buildPlayerRequestURIFingerprint = legacyFingerprint( internal val buildPlayerRequestURIFingerprint = legacyFingerprint(
@ -28,11 +31,11 @@ internal val buildPlayerRequestURIFingerprint = legacyFingerprint(
"asig", "asig",
), ),
customFingerprint = { method, _ -> customFingerprint = { method, _ ->
indexOfToStringInstruction(method) >= 0 indexOfUriToStringInstruction(method) >= 0
}, },
) )
internal fun indexOfToStringInstruction(method: Method) = internal fun indexOfUriToStringInstruction(method: Method) =
method.indexOfFirstInstruction { method.indexOfFirstInstruction {
opcode == Opcode.INVOKE_VIRTUAL && opcode == Opcode.INVOKE_VIRTUAL &&
getReference<MethodReference>().toString() == "Landroid/net/Uri;->toString()Ljava/lang/String;" getReference<MethodReference>().toString() == "Landroid/net/Uri;->toString()Ljava/lang/String;"