From e3d44458eb0285f3a55a72bcbf179bdf30875aa6 Mon Sep 17 00:00:00 2001 From: reis Date: Sun, 22 Jan 2023 19:18:55 +0300 Subject: [PATCH] feat(youtube/microg-support): check if Vanced MicroG is running in the background (#301) Signed-off-by: oSumAtrIX Co-authored-by: oSumAtrIX --- .../integrations/patches/MicroGSupport.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/integrations/java/app/revanced/integrations/patches/MicroGSupport.java b/integrations/java/app/revanced/integrations/patches/MicroGSupport.java index f7aaaac0f..2d1467426 100644 --- a/integrations/java/app/revanced/integrations/patches/MicroGSupport.java +++ b/integrations/java/app/revanced/integrations/patches/MicroGSupport.java @@ -2,11 +2,14 @@ package app.revanced.integrations.patches; import static app.revanced.integrations.sponsorblock.StringRef.str; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.widget.Toast; +import java.util.Objects; + import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; @@ -14,21 +17,32 @@ public class MicroGSupport { private static final String MICROG_VENDOR = "com.mgoogle"; private static final String MICROG_PACKAGE_NAME = "com.mgoogle.android.gms"; private static final String VANCED_MICROG_DOWNLOAD_LINK = "https://github.com/TeamVanced/VancedMicroG/releases/latest"; + private static final String DONT_KILL_MY_APP_LINK = "https://dontkillmyapp.com"; + private static final Uri VANCED_MICROG_PROVIDER = Uri.parse("content://com.mgoogle.android.gsf.gservices/prefix"); + + private static void startIntent(Context context, String uriString, String message) { + Toast.makeText(context, message, Toast.LENGTH_LONG).show(); + + var intent = new Intent(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setData(Uri.parse(uriString)); + context.startActivity(intent); + } public static void checkAvailability() { - var context = ReVancedUtils.getContext(); - assert context != null; + var context = Objects.requireNonNull(ReVancedUtils.getContext()); + try { context.getPackageManager().getPackageInfo(MICROG_PACKAGE_NAME, PackageManager.GET_ACTIVITIES); - LogHelper.printDebug(() -> "MicroG is installed on the device"); } catch (PackageManager.NameNotFoundException exception) { - LogHelper.printException(() -> ("MicroG was not found"), exception); - Toast.makeText(context, str("microg_not_installed_warning"), Toast.LENGTH_LONG).show(); + LogHelper.printException(() -> ("Vanced MicroG was not found"), exception); + startIntent(context, VANCED_MICROG_DOWNLOAD_LINK, str("microg_not_installed_warning")); + } - var intent = new Intent(Intent.ACTION_VIEW); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.setData(Uri.parse(VANCED_MICROG_DOWNLOAD_LINK)); - context.startActivity(intent); + try (var client = context.getContentResolver().acquireContentProviderClient(VANCED_MICROG_PROVIDER)) { + if (client != null) return; + LogHelper.printException(() -> ("Vanced MicroG is not running in the background")); + startIntent(context, DONT_KILL_MY_APP_LINK, str("microg_not_running_warning")); } } }