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")
public class ReturnYouTubeUsernamePatch {
private static final boolean RETURN_YOUTUBE_USERNAME_ENABLED = BaseSettings.RETURN_YOUTUBE_USERNAME_ENABLED.get();
private static final Boolean RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT = BaseSettings.RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT.get().userNameFirst;
private static final String YOUTUBE_API_KEY = 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 volatile String lastFetchedHandle = "";
/**
* Injection point.
@ -51,18 +53,12 @@ public class ReturnYouTubeUsernamePatch {
if (!RETURN_YOUTUBE_USERNAME_ENABLED) {
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].
if (!conversionContext.toString().contains(AUTHOR_BADGE_PATH)) {
return original;
}
String handle = original.toString();
if (fetchNeeded && !handle.equals(lastFetchedHandle)) {
lastFetchedHandle = handle;
// Get the original username using YouTube Data API v3.
if (fetchNeeded) {
ChannelRequest.fetchRequestIfNeeded(handle, YOUTUBE_API_KEY, RETURN_YOUTUBE_USERNAME_DISPLAY_FORMAT);
return original;
}
@ -77,9 +73,7 @@ public class ReturnYouTubeUsernamePatch {
Logger.printDebug(() -> "ChannelRequest Stream is null, handle:" + handle);
return original;
}
final CharSequence copiedSpannableString = copySpannableString(original, userName);
Logger.printDebug(() -> "Replaced: '" + original + "' with: '" + copiedSpannableString + "'");
return copiedSpannableString;
return copySpannableString(original, userName);
} catch (Exception ex) {
Logger.printException(() -> "onLithoTextLoaded failure", ex);
}

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import app.revanced.extension.shared.requests.Route;
public class ChannelRoutes {
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() {
}

View File

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

View File

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