2022-06-24 00:16:32 +02:00
|
|
|
package app.revanced.integrations.utils;
|
|
|
|
|
2022-11-25 00:10:58 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2022-06-24 00:16:32 +02:00
|
|
|
import android.content.Context;
|
2022-06-27 22:08:50 +02:00
|
|
|
import android.content.res.Resources;
|
2022-06-24 00:16:32 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
|
2022-06-27 22:08:50 +02:00
|
|
|
import app.revanced.integrations.sponsorblock.player.PlayerType;
|
2022-06-24 00:16:32 +02:00
|
|
|
|
|
|
|
public class ReVancedUtils {
|
|
|
|
|
2022-06-27 22:08:50 +02:00
|
|
|
private static PlayerType env;
|
2022-11-25 00:10:58 +01:00
|
|
|
private static boolean newVideo = false;
|
2022-06-27 22:08:50 +02:00
|
|
|
|
2022-11-25 00:10:58 +01:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
2022-06-27 22:08:50 +02:00
|
|
|
public static Context context;
|
2022-10-29 01:54:03 +02:00
|
|
|
|
2022-11-25 00:10:58 +01:00
|
|
|
private ReVancedUtils() {
|
|
|
|
} // utility class
|
|
|
|
|
2022-10-29 01:54:03 +02:00
|
|
|
public static boolean containsAny(final String value, final String... targets) {
|
|
|
|
for (String string : targets)
|
2022-11-19 23:22:34 +01:00
|
|
|
if (!string.isEmpty() && value.contains(string)) return true;
|
2022-10-29 01:54:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-27 22:08:50 +02:00
|
|
|
|
2022-07-05 22:31:13 +02:00
|
|
|
public static void setNewVideo(boolean started) {
|
2022-11-30 00:49:26 +01:00
|
|
|
LogHelper.printDebug(() -> "New video started: " + started);
|
2022-07-05 22:31:13 +02:00
|
|
|
newVideo = started;
|
|
|
|
}
|
2022-10-29 01:54:03 +02:00
|
|
|
|
2022-07-05 22:31:13 +02:00
|
|
|
public static boolean isNewVideoStarted() {
|
|
|
|
return newVideo;
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:29:39 +02:00
|
|
|
public static Integer getResourceIdByName(Context context, String type, String name) {
|
|
|
|
try {
|
|
|
|
Resources res = context.getResources();
|
|
|
|
return res.getIdentifier(name, type, context.getPackageName());
|
|
|
|
} catch (Throwable exception) {
|
2022-11-30 00:49:26 +01:00
|
|
|
LogHelper.printException(() -> ("Resource not found."), exception);
|
2022-07-11 14:29:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 22:08:50 +02:00
|
|
|
public static void setPlayerType(PlayerType type) {
|
|
|
|
env = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PlayerType getPlayerType() {
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2022-06-24 00:16:32 +02:00
|
|
|
public static int getIdentifier(String name, String defType) {
|
2022-06-27 22:08:50 +02:00
|
|
|
Context context = getContext();
|
2022-06-24 00:16:32 +02:00
|
|
|
return context.getResources().getIdentifier(name, defType, context.getPackageName());
|
|
|
|
}
|
|
|
|
|
2022-06-27 22:08:50 +02:00
|
|
|
public static Context getContext() {
|
|
|
|
if (context != null) {
|
|
|
|
return context;
|
|
|
|
} else {
|
2022-11-30 00:49:26 +01:00
|
|
|
LogHelper.printException(() -> ("Context is null, returning null!"));
|
2022-06-27 22:08:50 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 18:42:43 +02:00
|
|
|
|
|
|
|
public static boolean isTablet(Context context) {
|
|
|
|
return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
|
|
|
|
}
|
2022-11-25 00:10:58 +01:00
|
|
|
|
|
|
|
public static void runOnMainThread(Runnable runnable) {
|
|
|
|
new Handler(Looper.getMainLooper()).post(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return if the calling thread is on the main thread
|
|
|
|
*/
|
|
|
|
public static boolean currentIsOnMainThread() {
|
|
|
|
return Looper.getMainLooper().isCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws IllegalStateException if the calling thread is _not_ on the main thread
|
|
|
|
*/
|
|
|
|
public static void verifyOnMainThread() throws IllegalStateException {
|
|
|
|
if (!currentIsOnMainThread()) {
|
|
|
|
throw new IllegalStateException("Must call _on_ the main thread");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws IllegalStateException if the calling thread _is_ on the main thread
|
|
|
|
*/
|
|
|
|
public static void verifyOffMainThread() throws IllegalStateException {
|
|
|
|
if (currentIsOnMainThread()) {
|
|
|
|
throw new IllegalStateException("Must call _off_ the main thread");
|
|
|
|
}
|
|
|
|
}
|
2022-06-24 00:16:32 +02:00
|
|
|
}
|