This repository was archived by the owner on Dec 8, 2024. It is now read-only.
forked from ReVanced/revanced-integrations
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(YouTube): add
Hook YouTube Music actions
patch
- Loading branch information
Showing
3 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...main/java/app/revanced/integrations/youtube/patches/general/YouTubeMusicActionsPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package app.revanced.integrations.youtube.patches.general; | ||
|
||
import static app.revanced.integrations.shared.utils.StringRef.str; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import app.revanced.integrations.shared.settings.Setting; | ||
import app.revanced.integrations.youtube.settings.Settings; | ||
import app.revanced.integrations.youtube.settings.preference.ThirdPartyYouTubeMusicPreference; | ||
import app.revanced.integrations.youtube.utils.ExtendedUtils; | ||
import app.revanced.integrations.youtube.utils.VideoUtils; | ||
|
||
@SuppressWarnings("unused") | ||
public final class YouTubeMusicActionsPatch extends VideoUtils { | ||
|
||
private static final String PACKAGE_NAME_YOUTUBE_MUSIC = "com.google.android.apps.youtube.music"; | ||
|
||
private static final boolean isOverrideYouTubeMusicEnabled = | ||
Settings.OVERRIDE_YOUTUBE_MUSIC_BUTTON.get(); | ||
|
||
private static final boolean overrideYouTubeMusicEnabled = | ||
isOverrideYouTubeMusicEnabled && isYouTubeMusicEnabled(); | ||
|
||
public static String overridePackageName(@NonNull String packageName) { | ||
if (!overrideYouTubeMusicEnabled) { | ||
return packageName; | ||
} | ||
if (!StringUtils.equals(PACKAGE_NAME_YOUTUBE_MUSIC, packageName)) { | ||
return packageName; | ||
} | ||
final String thirdPartyPackageName = Settings.THIRD_PARTY_YOUTUBE_MUSIC_PACKAGE_NAME.get(); | ||
if (!ExtendedUtils.isPackageEnabled(thirdPartyPackageName)) { | ||
return packageName; | ||
} | ||
return thirdPartyPackageName; | ||
} | ||
|
||
private static boolean isYouTubeMusicEnabled() { | ||
return ExtendedUtils.isPackageEnabled(PACKAGE_NAME_YOUTUBE_MUSIC); | ||
} | ||
|
||
// Modified by a patch. Do not touch. | ||
public static String getRVXMusicPackageName() { | ||
return "com.google.android.apps.youtube.music"; | ||
} | ||
|
||
public static final class HookYouTubeMusicAvailability implements Setting.Availability { | ||
@Override | ||
public boolean isAvailable() { | ||
return isYouTubeMusicEnabled(); | ||
} | ||
} | ||
|
||
public static final class HookYouTubeMusicPackageNameAvailability implements Setting.Availability { | ||
@Override | ||
public boolean isAvailable() { | ||
return isOverrideYouTubeMusicEnabled && isYouTubeMusicEnabled(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...p/revanced/integrations/youtube/settings/preference/ThirdPartyYouTubeMusicPreference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package app.revanced.integrations.youtube.settings.preference; | ||
|
||
import static app.revanced.integrations.shared.utils.StringRef.str; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.AlertDialog; | ||
import android.content.Context; | ||
import android.preference.Preference; | ||
import android.text.Editable; | ||
import android.text.TextWatcher; | ||
import android.util.AttributeSet; | ||
import android.util.TypedValue; | ||
import android.widget.EditText; | ||
import android.widget.LinearLayout; | ||
import android.widget.TableLayout; | ||
import android.widget.TableRow; | ||
|
||
import java.util.Arrays; | ||
|
||
import app.revanced.integrations.shared.settings.StringSetting; | ||
import app.revanced.integrations.shared.utils.ResourceUtils; | ||
import app.revanced.integrations.shared.utils.Utils; | ||
import app.revanced.integrations.youtube.settings.Settings; | ||
import app.revanced.integrations.youtube.utils.ExtendedUtils; | ||
|
||
@SuppressWarnings({"unused", "deprecation"}) | ||
public class ThirdPartyYouTubeMusicPreference extends Preference implements Preference.OnPreferenceClickListener { | ||
|
||
private static final StringSetting settings = Settings.THIRD_PARTY_YOUTUBE_MUSIC_PACKAGE_NAME; | ||
private static final String[] mEntries = ResourceUtils.getStringArray("revanced_third_party_youtube_music_label"); | ||
private static final String[] mEntryValues = ResourceUtils.getStringArray("revanced_third_party_youtube_music_package_name"); | ||
|
||
@SuppressLint("StaticFieldLeak") | ||
private static EditText mEditText; | ||
private static String packageName; | ||
private static int mClickedDialogEntryIndex; | ||
|
||
private final TextWatcher textWatcher = new TextWatcher() { | ||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
} | ||
|
||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
} | ||
|
||
public void afterTextChanged(Editable s) { | ||
packageName = s.toString(); | ||
mClickedDialogEntryIndex = Arrays.asList(mEntryValues).indexOf(packageName); | ||
} | ||
}; | ||
|
||
private void init() { | ||
setSelectable(true); | ||
setOnPreferenceClickListener(this); | ||
} | ||
|
||
public ThirdPartyYouTubeMusicPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
init(); | ||
} | ||
|
||
public ThirdPartyYouTubeMusicPreference(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(); | ||
} | ||
|
||
public ThirdPartyYouTubeMusicPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
public ThirdPartyYouTubeMusicPreference(Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceClick(Preference preference) { | ||
packageName = settings.get(); | ||
mClickedDialogEntryIndex = Arrays.asList(mEntryValues).indexOf(packageName); | ||
|
||
final Context context = getContext(); | ||
AlertDialog.Builder builder = Utils.getEditTextDialogBuilder(context); | ||
|
||
TableLayout table = new TableLayout(context); | ||
table.setOrientation(LinearLayout.HORIZONTAL); | ||
table.setPadding(15, 0, 15, 0); | ||
|
||
TableRow row = new TableRow(context); | ||
|
||
mEditText = new EditText(context); | ||
mEditText.setHint(settings.defaultValue); | ||
mEditText.setText(packageName); | ||
mEditText.addTextChangedListener(textWatcher); | ||
mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PT, 9); | ||
mEditText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f)); | ||
row.addView(mEditText); | ||
|
||
table.addView(row); | ||
builder.setView(table); | ||
|
||
builder.setTitle(str("revanced_third_party_youtube_music_dialog_title")); | ||
builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, (dialog, which) -> { | ||
mClickedDialogEntryIndex = which; | ||
mEditText.setText(mEntryValues[which]); | ||
}); | ||
builder.setPositiveButton(android.R.string.ok, (dialog, which) -> { | ||
final String packageName = mEditText.getText().toString().trim(); | ||
settings.save(packageName); | ||
checkPackageIsValid(context, packageName); | ||
dialog.dismiss(); | ||
}); | ||
builder.setNeutralButton(str("revanced_extended_settings_reset"), (dialog, which) -> settings.resetToDefault()); | ||
builder.setNegativeButton(android.R.string.cancel, null); | ||
|
||
builder.show(); | ||
|
||
return true; | ||
} | ||
|
||
private static void checkPackageIsValid(Context context, String packageName) { | ||
if (packageName.isEmpty()) { | ||
settings.resetToDefault(); | ||
return; | ||
} | ||
|
||
String appName = ""; | ||
if (mClickedDialogEntryIndex >= 0) { | ||
appName = mEntries[mClickedDialogEntryIndex]; | ||
} | ||
|
||
showToastOrOpenWebsites(context, appName, packageName); | ||
} | ||
|
||
private static void showToastOrOpenWebsites(Context context, String appName, String packageName) { | ||
if (ExtendedUtils.isPackageEnabled(packageName)) { | ||
return; | ||
} | ||
|
||
Utils.showToastShort(str("revanced_third_party_youtube_music_not_installed_warning", appName.isEmpty() ? packageName : appName)); | ||
} | ||
|
||
} |