From 1a47780ab466a3493d078e3e79087a27289d2360 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 22 Jun 2021 15:25:51 +0200 Subject: [PATCH] Android: Don't use getEmulationActivity in GCAdapter/Wiimote code The app context should work just as well as an activity context. Also moving a hardcoded string to strings.xml. --- .../dolphinemu/utils/Java_GCAdapter.java | 47 ++++++------------- .../dolphinemu/utils/Java_WiimoteAdapter.java | 35 ++++++-------- .../app/src/main/res/values/strings.xml | 1 + 3 files changed, 30 insertions(+), 53 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_GCAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_GCAdapter.java index 7d0af9e63c..0fbc931b8c 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_GCAdapter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_GCAdapter.java @@ -1,6 +1,5 @@ package org.dolphinemu.dolphinemu.utils; -import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -16,7 +15,8 @@ import android.widget.Toast; import androidx.annotation.Keep; -import org.dolphinemu.dolphinemu.NativeLibrary; +import org.dolphinemu.dolphinemu.DolphinApplication; +import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.services.USBPermService; import java.util.HashMap; @@ -36,33 +36,25 @@ public class Java_GCAdapter private static void RequestPermission() { - Context context = NativeLibrary.getEmulationActivity(); - if (context != null) + HashMap devices = manager.getDeviceList(); + for (Map.Entry pair : devices.entrySet()) { - HashMap devices = manager.getDeviceList(); - for (Map.Entry pair : devices.entrySet()) + UsbDevice dev = pair.getValue(); + if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e) { - UsbDevice dev = pair.getValue(); - if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e) + if (!manager.hasPermission(dev)) { - if (!manager.hasPermission(dev)) - { - Intent intent = new Intent(context, USBPermService.class); + Context context = DolphinApplication.getAppContext(); + Intent intent = new Intent(context, USBPermService.class); - int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? - PendingIntent.FLAG_IMMUTABLE : 0; - PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); + int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? + PendingIntent.FLAG_IMMUTABLE : 0; + PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); - manager.requestPermission(dev, pendingIntent); - } + manager.requestPermission(dev, pendingIntent); } } } - else - { - Log.warning("Cannot request GameCube Adapter permission as EmulationActivity is null."); - } - } public static void Shutdown() @@ -153,17 +145,8 @@ public class Java_GCAdapter } } - final Activity emulationActivity = NativeLibrary.getEmulationActivity(); - if (emulationActivity != null) - { - emulationActivity.runOnUiThread(() -> Toast.makeText(emulationActivity, - "GameCube Adapter couldn't be opened. Please re-plug the device.", - Toast.LENGTH_LONG).show()); - } - else - { - Log.warning("Cannot show toast for GameCube Adapter failure."); - } + Toast.makeText(DolphinApplication.getAppContext(), R.string.replug_gc_adapter, + Toast.LENGTH_LONG).show(); usb_con.close(); } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_WiimoteAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_WiimoteAdapter.java index f3e5e5274c..ba6ba1e86c 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_WiimoteAdapter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Java_WiimoteAdapter.java @@ -13,7 +13,7 @@ import android.os.Build; import androidx.annotation.Keep; -import org.dolphinemu.dolphinemu.NativeLibrary; +import org.dolphinemu.dolphinemu.DolphinApplication; import org.dolphinemu.dolphinemu.services.USBPermService; import java.util.Arrays; @@ -38,35 +38,28 @@ public class Java_WiimoteAdapter private static void RequestPermission() { - Context context = NativeLibrary.getEmulationActivity(); - if (context != null) + HashMap devices = manager.getDeviceList(); + for (Map.Entry pair : devices.entrySet()) { - HashMap devices = manager.getDeviceList(); - for (Map.Entry pair : devices.entrySet()) + UsbDevice dev = pair.getValue(); + if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && + dev.getVendorId() == NINTENDO_VENDOR_ID) { - UsbDevice dev = pair.getValue(); - if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && - dev.getVendorId() == NINTENDO_VENDOR_ID) + if (!manager.hasPermission(dev)) { - if (!manager.hasPermission(dev)) - { - Log.warning("Requesting permission for Wii Remote adapter"); + Log.warning("Requesting permission for Wii Remote adapter"); - Intent intent = new Intent(context, USBPermService.class); + Context context = DolphinApplication.getAppContext(); + Intent intent = new Intent(context, USBPermService.class); - int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? - PendingIntent.FLAG_IMMUTABLE : 0; - PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); + int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? + PendingIntent.FLAG_IMMUTABLE : 0; + PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); - manager.requestPermission(dev, pendingIntent); - } + manager.requestPermission(dev, pendingIntent); } } } - else - { - Log.warning("Cannot request Wiimote adapter permission as EmulationActivity is null."); - } } @Keep diff --git a/Source/Android/app/src/main/res/values/strings.xml b/Source/Android/app/src/main/res/values/strings.xml index 93c9c0c030..b910579121 100644 --- a/Source/Android/app/src/main/res/values/strings.xml +++ b/Source/Android/app/src/main/res/values/strings.xml @@ -466,6 +466,7 @@ It can efficiently compress both junk data and encrypted Wii data. Default Values %1$d%2$s Disc %1$d + GameCube Adapter couldn\'t be opened. Please re-plug the device. GameCube Controller 1 is set to \"None\" Ignore for this session