fix(youtube/remember-video-quality): treat any connection as wifi except mobile and bluetooth

This commit is contained in:
oSumAtrIX 2023-03-06 09:19:18 +01:00
parent 0c725218fd
commit 1f90f7b9cc
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -1,21 +1,18 @@
package app.revanced.integrations.patches.playback.quality;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.SharedPrefHelper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.SharedPrefHelper;
public class RememberVideoQualityPatch {
public static int selectedQuality1 = -2;
@ -24,38 +21,37 @@ public class RememberVideoQualityPatch {
public static void changeDefaultQuality(int defaultQuality) {
Context context = ReVancedUtils.getContext();
if (isConnectedWifi(context)) {
try {
SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", defaultQuality + "");
String message = "Changing default Wi-Fi quality to: " + defaultQuality;
LogHelper.printDebug(() -> message);
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to change default WI-FI quality", ex);
}
} else if (isConnectedMobile(context)) {
try {
SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", defaultQuality + "");
String message = "Changing default mobile data quality to:" + defaultQuality;
LogHelper.printDebug(() -> message);
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to change default mobile data quality", ex);
}
} else {
var networkType = getNetworType(context);
if (networkType == NetworkType.NONE) {
String message = "No internet connection.";
LogHelper.printDebug(() -> message);
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} else {
var preferenceKey = "wifi_quality";
var networkTypeMessage = "WIFI";
if (networkType == NetworkType.MOBILE) {
networkTypeMessage = "mobile";
preferenceKey = "mobile_quality";
}
SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, preferenceKey, defaultQuality + "");
String message = "Changing default" + networkTypeMessage + "quality to:" + defaultQuality;
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
userChangedQuality = false;
}
public static int setVideoQuality(Object[] qualities, int quality, Object qInterface, String qIndexMethod) {
int preferredQuality;
Field[] fields;
if (!(newVideo || userChangedQuality) || qInterface == null) {
return quality;
}
Class<?> intType = Integer.TYPE;
ArrayList<Integer> iStreamQualities = new ArrayList<>();
try {
@ -93,51 +89,48 @@ public class RememberVideoQualityPatch {
LogHelper.printException(() -> "Context is null or settings not initialized, returning quality: " + qualityToLog);
return quality;
}
if (isConnectedWifi(context)) {
preferredQuality = SharedPrefHelper.getInt(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", -2);
LogHelper.printDebug(() -> "Wi-Fi connection detected, preferred quality: " + preferredQuality);
} else if (isConnectedMobile(context)) {
preferredQuality = SharedPrefHelper.getInt(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", -2);
LogHelper.printDebug(() -> "Mobile data connection detected, preferred quality: " + preferredQuality);
} else {
var networkType = getNetworType(context);
if (networkType == NetworkType.NONE) {
LogHelper.printDebug(() -> "No Internet connection!");
return quality;
}
if (preferredQuality == -2) {
return quality;
}
for (int streamQuality2 : iStreamQualities) {
final int indexToLog = index;
LogHelper.printDebug(() -> "Quality at index " + indexToLog + ": " + streamQuality2);
index++;
}
for (Integer iStreamQuality : iStreamQualities) {
int streamQuality3 = iStreamQuality;
if (streamQuality3 <= preferredQuality) {
quality = streamQuality3;
} else {
var preferenceKey = "wifi_quality";
if (networkType == NetworkType.MOBILE) preferenceKey = "mobile_quality";
int preferredQuality = SharedPrefHelper.getInt(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, preferenceKey, -2);
if (preferredQuality == -2) return quality;
for (int streamQuality2 : iStreamQualities) {
final int indexToLog = index;
LogHelper.printDebug(() -> "Quality at index " + indexToLog + ": " + streamQuality2);
index++;
}
for (Integer iStreamQuality : iStreamQualities) {
int streamQuality3 = iStreamQuality;
if (streamQuality3 <= preferredQuality) {
quality = streamQuality3;
}
}
if (quality == -2) return quality;
int qualityIndex = iStreamQualities.indexOf(quality);
final int qualityToLog2 = quality;
LogHelper.printDebug(() -> "Index of quality " + qualityToLog2 + " is " + qualityIndex);
try {
Class<?> cl = qInterface.getClass();
Method m = cl.getMethod(qIndexMethod, Integer.TYPE);
LogHelper.printDebug(() -> "Method is: " + qIndexMethod);
m.invoke(qInterface, iStreamQualities.get(qualityIndex));
LogHelper.printDebug(() -> "Quality changed to: " + qualityIndex);
return qualityIndex;
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to set quality", ex);
return qualityIndex;
}
}
if (quality == -2) {
return quality;
}
int qualityIndex = iStreamQualities.indexOf(quality);
final int qualityToLog2 = quality;
LogHelper.printDebug(() -> "Index of quality " + qualityToLog2 + " is " + qualityIndex);
try {
Class<?> cl = qInterface.getClass();
Method m = cl.getMethod(qIndexMethod, Integer.TYPE);
LogHelper.printDebug(() -> "Method is: " + qIndexMethod);
m.invoke(qInterface, iStreamQualities.get(qualityIndex));
LogHelper.printDebug(() -> "Quality changed to: " + qualityIndex);
return qualityIndex;
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to set quality", ex);
return qualityIndex;
}
}
public static void userChangedQuality(int selectedQuality) {
// Do not remember a **new** quality if REMEMBER_VIDEO_QUALITY is false
if (!SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean()) return;
selectedQuality1 = selectedQuality;
@ -148,20 +141,23 @@ public class RememberVideoQualityPatch {
newVideo = true;
}
@SuppressLint("MissingPermission")
private static NetworkInfo getNetworkInfo(Context context) {
private static NetworkType getNetworType(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
var networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return NetworkType.NONE;
} else {
var type = networkInfo.getType();
return type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_BLUETOOTH ? NetworkType.MOBILE : NetworkType.OTHER;
}
}
private static boolean isConnectedWifi(Context context) {
NetworkInfo info = getNetworkInfo(context);
return info != null && info.isConnected() && info.getType() == 1;
}
private static boolean isConnectedMobile(Context context) {
NetworkInfo info = getNetworkInfo(context);
return info != null && info.isConnected() && info.getType() == 0;
enum NetworkType {
MOBILE,
OTHER,
NONE
}
}