Merge pull request #9836 from JosJuice/android-getemulationactivity

Android: Don't use getEmulationActivity in GCAdapter/Wiimote code
This commit is contained in:
Mai M 2021-06-22 11:59:25 -04:00 committed by GitHub
commit be2ec728e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 53 deletions

View File

@ -1,6 +1,5 @@
package org.dolphinemu.dolphinemu.utils; package org.dolphinemu.dolphinemu.utils;
import android.app.Activity;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -16,7 +15,8 @@ import android.widget.Toast;
import androidx.annotation.Keep; 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 org.dolphinemu.dolphinemu.services.USBPermService;
import java.util.HashMap; import java.util.HashMap;
@ -36,33 +36,25 @@ public class Java_GCAdapter
private static void RequestPermission() private static void RequestPermission()
{ {
Context context = NativeLibrary.getEmulationActivity(); HashMap<String, UsbDevice> devices = manager.getDeviceList();
if (context != null) for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{ {
HashMap<String, UsbDevice> devices = manager.getDeviceList(); UsbDevice dev = pair.getValue();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet()) if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
{ {
UsbDevice dev = pair.getValue(); if (!manager.hasPermission(dev))
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
{ {
if (!manager.hasPermission(dev)) Context context = DolphinApplication.getAppContext();
{ Intent intent = new Intent(context, USBPermService.class);
Intent intent = new Intent(context, USBPermService.class);
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
PendingIntent.FLAG_IMMUTABLE : 0; PendingIntent.FLAG_IMMUTABLE : 0;
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); 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() public static void Shutdown()
@ -153,17 +145,8 @@ public class Java_GCAdapter
} }
} }
final Activity emulationActivity = NativeLibrary.getEmulationActivity(); Toast.makeText(DolphinApplication.getAppContext(), R.string.replug_gc_adapter,
if (emulationActivity != null) Toast.LENGTH_LONG).show();
{
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.");
}
usb_con.close(); usb_con.close();
} }
} }

View File

@ -13,7 +13,7 @@ import android.os.Build;
import androidx.annotation.Keep; import androidx.annotation.Keep;
import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.DolphinApplication;
import org.dolphinemu.dolphinemu.services.USBPermService; import org.dolphinemu.dolphinemu.services.USBPermService;
import java.util.Arrays; import java.util.Arrays;
@ -38,35 +38,28 @@ public class Java_WiimoteAdapter
private static void RequestPermission() private static void RequestPermission()
{ {
Context context = NativeLibrary.getEmulationActivity(); HashMap<String, UsbDevice> devices = manager.getDeviceList();
if (context != null) for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{ {
HashMap<String, UsbDevice> devices = manager.getDeviceList(); UsbDevice dev = pair.getValue();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet()) if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID &&
dev.getVendorId() == NINTENDO_VENDOR_ID)
{ {
UsbDevice dev = pair.getValue(); if (!manager.hasPermission(dev))
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID &&
dev.getVendorId() == NINTENDO_VENDOR_ID)
{ {
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 ? int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
PendingIntent.FLAG_IMMUTABLE : 0; PendingIntent.FLAG_IMMUTABLE : 0;
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, flags); 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 @Keep

View File

@ -477,6 +477,7 @@ It can efficiently compress both junk data and encrypted Wii data.
<string name="default_values">Default Values</string> <string name="default_values">Default Values</string>
<string name="slider_setting_value">%1$d%2$s</string> <string name="slider_setting_value">%1$d%2$s</string>
<string name="disc_number">Disc %1$d</string> <string name="disc_number">Disc %1$d</string>
<string name="replug_gc_adapter">GameCube Adapter couldn\'t be opened. Please re-plug the device.</string>
<string name="disabled_gc_overlay_notice">GameCube Controller 1 is set to \"None\"</string> <string name="disabled_gc_overlay_notice">GameCube Controller 1 is set to \"None\"</string>
<string name="ignore_warning_alert_messages">Ignore for this session</string> <string name="ignore_warning_alert_messages">Ignore for this session</string>
<string name="make_sure_continuous_scan_enabled">Please make sure Continuous Scanning is enabled in Core Settings.</string> <string name="make_sure_continuous_scan_enabled">Please make sure Continuous Scanning is enabled in Core Settings.</string>