Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(youtube/video-speed): change custom video speeds inside app sett…
…ings (#393)
- Loading branch information
1 parent
3645a4a
commit b42790f
Showing
5 changed files
with
105 additions
and
42 deletions.
There are no files selected for viewing
103 changes: 97 additions & 6 deletions
103
...src/main/java/app/revanced/integrations/patches/playback/speed/CustomVideoSpeedPatch.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 |
---|---|---|
@@ -1,12 +1,103 @@ | ||
package app.revanced.integrations.patches.playback.speed; | ||
|
||
import android.preference.ListPreference; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.Arrays; | ||
|
||
import app.revanced.integrations.settings.SettingsEnum; | ||
import app.revanced.integrations.utils.LogHelper; | ||
import app.revanced.integrations.utils.ReVancedUtils; | ||
|
||
public class CustomVideoSpeedPatch { | ||
/** | ||
* Default playback speeds offered by YouTube. | ||
* Values are also used by {@link RememberPlaybackSpeedPatch}. | ||
* | ||
* If custom video speed is applied, | ||
* then this array is overwritten by the patch with custom speeds | ||
* Maximum playback speed, exclusive value. Custom speeds must be less than this value. | ||
*/ | ||
public static final float MAXIMUM_PLAYBACK_SPEED = 10; | ||
|
||
/** | ||
* Custom playback speeds. | ||
*/ | ||
public static float[] customVideoSpeeds; | ||
|
||
/** | ||
* Minimum value of {@link #customVideoSpeeds} | ||
*/ | ||
public static float minVideoSpeed; | ||
|
||
/** | ||
* Maxium value of {@link #customVideoSpeeds} | ||
*/ | ||
public static float maxVideoSpeed; | ||
|
||
/** | ||
* PreferenceList entries and values, of all available playback speeds. | ||
*/ | ||
private static String[] preferenceListEntries, preferenceListEntryValues; | ||
|
||
static { | ||
loadSpeeds(); | ||
} | ||
|
||
private static void resetCustomSpeeds(@NonNull String toastMessage) { | ||
ReVancedUtils.showToastLong(toastMessage); | ||
SettingsEnum.PLAYBACK_SPEED_AVAILABLE.saveValue(SettingsEnum.PLAYBACK_SPEED_AVAILABLE.defaultValue); | ||
} | ||
|
||
private static void loadSpeeds() { | ||
try { | ||
String[] speedStrings = SettingsEnum.PLAYBACK_SPEED_AVAILABLE.getString().split("\\s+"); | ||
Arrays.sort(speedStrings); | ||
if (speedStrings.length == 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
customVideoSpeeds = new float[speedStrings.length]; | ||
for (int i = 0, length = speedStrings.length; i < length; i++) { | ||
final float speed = Float.parseFloat(speedStrings[i]); | ||
if (speed <= 0 || arrayContains(customVideoSpeeds, speed)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (speed >= MAXIMUM_PLAYBACK_SPEED) { | ||
resetCustomSpeeds("Custom speeds must be less than " + MAXIMUM_PLAYBACK_SPEED | ||
+ ". Using default values."); | ||
loadSpeeds(); | ||
return; | ||
} | ||
minVideoSpeed = Math.min(minVideoSpeed, speed); | ||
maxVideoSpeed = Math.max(maxVideoSpeed, speed); | ||
customVideoSpeeds[i] = speed; | ||
} | ||
} catch (Exception ex) { | ||
LogHelper.printInfo(() -> "parse error", ex); | ||
resetCustomSpeeds("Invalid custom video speeds. Using default values."); | ||
loadSpeeds(); | ||
} | ||
} | ||
|
||
private static boolean arrayContains(float[] array, float value) { | ||
for (float arrayValue : array) { | ||
if (arrayValue == value) return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Initialize a settings preference list with the available playback speeds. | ||
*/ | ||
public static final float[] videoSpeeds = {0.25f, 0.75f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f}; | ||
public static void initializeListPreference(ListPreference preference) { | ||
if (preferenceListEntries == null) { | ||
preferenceListEntries = new String[customVideoSpeeds.length]; | ||
preferenceListEntryValues = new String[customVideoSpeeds.length]; | ||
int i = 0; | ||
for (float speed : customVideoSpeeds) { | ||
String speedString = String.valueOf(speed); | ||
preferenceListEntries[i] = speedString + "x"; | ||
preferenceListEntryValues[i] = speedString; | ||
i++; | ||
} | ||
} | ||
preference.setEntries(preferenceListEntries); | ||
preference.setEntryValues(preferenceListEntryValues); | ||
} | ||
} |
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
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
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
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