feat(YouTube): Add Override 'Open in YouTube Music' button patch
Co-authored-by: ILoveOpenSourceApplications <117499019+iloveopensourceapplications@users.noreply.github.com>
This commit is contained in:
parent
bc08ecf785
commit
8c2445f92f
6 changed files with 230 additions and 4 deletions
|
|
@ -373,6 +373,29 @@ public class Utils {
|
|||
return getContext().getResources().getStringArray(getResourceIdentifierOrThrow(ResourceType.ARRAY, resourceIdentifierName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specific app package is installed and enabled on the device.
|
||||
*
|
||||
* @param packageName The application package name to check (e.g., "app.morphe.android.apps.youtube.music").
|
||||
* @return True if the package is installed and enabled, false otherwise.
|
||||
*/
|
||||
public static boolean isPackageEnabled(String packageName) {
|
||||
Context context = getContext();
|
||||
if (context == null || !isNotEmpty(packageName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
return pm.getApplicationInfo(packageName, PackageManager.ApplicationInfoFlags.of(0)).enabled;
|
||||
} else {
|
||||
return pm.getApplicationInfo(packageName, 0).enabled;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public interface MatchFilter<T> {
|
||||
boolean matches(T object);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
package app.revanced.extension.youtube.patches;
|
||||
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import app.revanced.extension.shared.Utils;
|
||||
import app.revanced.extension.youtube.settings.Settings;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class OverrideOpenInYouTubeMusicButtonPatch {
|
||||
|
||||
private static final String YOUTUBE_MUSIC_PACKAGE_NAME = "com.google.android.apps.youtube.music";
|
||||
|
||||
private static final Boolean overrideButton = Settings.OVERRIDE_OPEN_IN_YOUTUBE_MUSIC_BUTTON.get();
|
||||
|
||||
private static final String overridePackageName = getOverridePackageName();
|
||||
|
||||
@SuppressWarnings("SameReturnValue")
|
||||
public static String getOverridePackageName() {
|
||||
return ""; // Value is replaced during patching.
|
||||
}
|
||||
|
||||
public static @Nullable Intent overrideSetPackage(@Nullable Intent intent, @Nullable String packageName) {
|
||||
if (intent == null || !overrideButton) return intent;
|
||||
|
||||
if (YOUTUBE_MUSIC_PACKAGE_NAME.equals(packageName)) {
|
||||
if (Utils.isNotEmpty(overridePackageName) && Utils.isPackageEnabled(overridePackageName)) {
|
||||
return intent.setPackage(overridePackageName);
|
||||
}
|
||||
|
||||
return intent.setPackage(null);
|
||||
}
|
||||
|
||||
return intent.setPackage(packageName);
|
||||
}
|
||||
|
||||
public static @Nullable Intent overrideSetData(@Nullable Intent intent, @Nullable Uri uri) {
|
||||
if (intent == null || uri == null || !overrideButton) return intent;
|
||||
|
||||
String uriString = uri.toString();
|
||||
if (uriString.contains(YOUTUBE_MUSIC_PACKAGE_NAME)) {
|
||||
if ("market".equals(uri.getScheme()) || uriString.contains("play.google.com/store/apps")) {
|
||||
intent.setData(Uri.parse("https://music.youtube.com/"));
|
||||
|
||||
if (Utils.isNotEmpty(overridePackageName) && Utils.isPackageEnabled(overridePackageName)) {
|
||||
intent.setPackage(overridePackageName);
|
||||
} else {
|
||||
intent.setPackage(null);
|
||||
}
|
||||
|
||||
return intent;
|
||||
}
|
||||
}
|
||||
|
||||
return intent.setData(uri);
|
||||
}
|
||||
}
|
||||
|
|
@ -296,6 +296,7 @@ public class Settings extends YouTubeAndMusicSettings {
|
|||
public static final BooleanSetting REMOVE_VIEWER_DISCRETION_DIALOG = new BooleanSetting("revanced_remove_viewer_discretion_dialog", FALSE,
|
||||
"revanced_remove_viewer_discretion_dialog_user_dialog_message");
|
||||
public static final BooleanSetting SPOOF_APP_VERSION = new BooleanSetting("revanced_spoof_app_version", FALSE, true, "revanced_spoof_app_version_user_dialog_message");
|
||||
public static final BooleanSetting OVERRIDE_OPEN_IN_YOUTUBE_MUSIC_BUTTON = new BooleanSetting("revanced_override_open_in_youtube_music_button", TRUE, true);
|
||||
public static final EnumSetting<StartPage> CHANGE_START_PAGE = new EnumSetting<>("revanced_change_start_page", StartPage.DEFAULT, true);
|
||||
public static final BooleanSetting CHANGE_START_PAGE_ALWAYS = new BooleanSetting("revanced_change_start_page_always", FALSE, true,
|
||||
new ChangeStartPageTypeAvailability());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue