Use weak references for the static Activity in NativeLibrary.
Add in null checks as well.
This commit is contained in:
parent
cde003c5cc
commit
80e1cc56b3
|
@ -12,13 +12,15 @@ import android.widget.Toast;
|
||||||
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
|
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
|
||||||
import org.dolphinemu.dolphinemu.utils.Log;
|
import org.dolphinemu.dolphinemu.utils.Log;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class which contains methods that interact
|
* Class which contains methods that interact
|
||||||
* with the native side of the Dolphin code.
|
* with the native side of the Dolphin code.
|
||||||
*/
|
*/
|
||||||
public final class NativeLibrary
|
public final class NativeLibrary
|
||||||
{
|
{
|
||||||
public static EmulationActivity sEmulationActivity;
|
public static WeakReference<EmulationActivity> sEmulationActivity = new WeakReference<>(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Button type for use in onTouchEvent
|
* Button type for use in onTouchEvent
|
||||||
|
@ -379,31 +381,48 @@ public final class NativeLibrary
|
||||||
public static void displayAlertMsg(final String alert)
|
public static void displayAlertMsg(final String alert)
|
||||||
{
|
{
|
||||||
Log.error("[NativeLibrary] Alert: " + alert);
|
Log.error("[NativeLibrary] Alert: " + alert);
|
||||||
sEmulationActivity.runOnUiThread(new Runnable()
|
final EmulationActivity emulationActivity = sEmulationActivity.get();
|
||||||
|
if (emulationActivity != null)
|
||||||
{
|
{
|
||||||
@Override
|
emulationActivity.runOnUiThread(new Runnable()
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
Toast.makeText(sEmulationActivity, "Panic Alert: " + alert, Toast.LENGTH_LONG).show();
|
@Override
|
||||||
}
|
public void run()
|
||||||
});
|
{
|
||||||
|
Toast.makeText(emulationActivity, "Panic Alert: " + alert, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.warning("[NativeLibrary] EmulationActivity is null, can't do panic toast.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void endEmulationActivity()
|
public static void endEmulationActivity()
|
||||||
{
|
{
|
||||||
Log.verbose("[NativeLibrary] Ending EmulationActivity.");
|
Log.verbose("[NativeLibrary] Ending EmulationActivity.");
|
||||||
sEmulationActivity.exitWithAnimation();
|
EmulationActivity emulationActivity = sEmulationActivity.get();
|
||||||
|
if (emulationActivity != null)
|
||||||
|
{
|
||||||
|
emulationActivity.exitWithAnimation();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.warning("[NativeLibrary] EmulationActivity is null, can't end.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setEmulationActivity(EmulationActivity emulationActivity)
|
public static void setEmulationActivity(EmulationActivity emulationActivity)
|
||||||
{
|
{
|
||||||
Log.verbose("[NativeLibrary] Registering EmulationActivity.");
|
Log.verbose("[NativeLibrary] Registering EmulationActivity.");
|
||||||
sEmulationActivity = emulationActivity;
|
sEmulationActivity = new WeakReference<>(emulationActivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearEmulationActivity()
|
public static void clearEmulationActivity()
|
||||||
{
|
{
|
||||||
Log.verbose("[NativeLibrary] Unregistering EmulationActivity.");
|
Log.verbose("[NativeLibrary] Unregistering EmulationActivity.");
|
||||||
sEmulationActivity = null;
|
|
||||||
|
sEmulationActivity.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
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.Intent;
|
import android.content.Intent;
|
||||||
import android.hardware.usb.UsbConfiguration;
|
import android.hardware.usb.UsbConfiguration;
|
||||||
import android.hardware.usb.UsbConstants;
|
import android.hardware.usb.UsbConstants;
|
||||||
|
@ -15,7 +17,6 @@ import org.dolphinemu.dolphinemu.NativeLibrary;
|
||||||
import org.dolphinemu.dolphinemu.services.USBPermService;
|
import org.dolphinemu.dolphinemu.services.USBPermService;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Java_GCAdapter {
|
public class Java_GCAdapter {
|
||||||
|
@ -29,22 +30,31 @@ public class Java_GCAdapter {
|
||||||
|
|
||||||
private static void RequestPermission()
|
private static void RequestPermission()
|
||||||
{
|
{
|
||||||
HashMap<String, UsbDevice> devices = manager.getDeviceList();
|
Context context = NativeLibrary.sEmulationActivity.get();
|
||||||
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
|
if (context != null)
|
||||||
{
|
{
|
||||||
UsbDevice dev = pair.getValue();
|
HashMap<String, UsbDevice> devices = manager.getDeviceList();
|
||||||
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
|
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
|
||||||
{
|
{
|
||||||
if (!manager.hasPermission(dev))
|
UsbDevice dev = pair.getValue();
|
||||||
|
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
|
||||||
{
|
{
|
||||||
Intent intent = new Intent();
|
if (!manager.hasPermission(dev))
|
||||||
PendingIntent pend_intent;
|
{
|
||||||
intent.setClass(NativeLibrary.sEmulationActivity, USBPermService.class);
|
Intent intent = new Intent();
|
||||||
pend_intent = PendingIntent.getService(NativeLibrary.sEmulationActivity, 0, intent, 0);
|
PendingIntent pend_intent;
|
||||||
manager.requestPermission(dev, pend_intent);
|
intent.setClass(context, USBPermService.class);
|
||||||
|
pend_intent = PendingIntent.getService(context, 0, intent, 0);
|
||||||
|
manager.requestPermission(dev, pend_intent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.warning("Cannot request GameCube Adapter permission as EmulationActivity is null.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Shutdown()
|
public static void Shutdown()
|
||||||
|
@ -124,14 +134,22 @@ public class Java_GCAdapter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeLibrary.sEmulationActivity.runOnUiThread(new Runnable()
|
final Activity emulationActivity = NativeLibrary.sEmulationActivity.get();
|
||||||
|
if (emulationActivity != null)
|
||||||
{
|
{
|
||||||
@Override
|
emulationActivity.runOnUiThread(new Runnable()
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
Toast.makeText(NativeLibrary.sEmulationActivity, "GameCube Adapter couldn't be opened. Please re-plug the device.", Toast.LENGTH_LONG).show();
|
@Override
|
||||||
}
|
public void run()
|
||||||
});
|
{
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.dolphinemu.dolphinemu.utils;
|
package org.dolphinemu.dolphinemu.utils;
|
||||||
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.hardware.usb.UsbConfiguration;
|
import android.hardware.usb.UsbConfiguration;
|
||||||
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbDevice;
|
||||||
|
@ -33,23 +34,31 @@ public class Java_WiimoteAdapter
|
||||||
|
|
||||||
private static void RequestPermission()
|
private static void RequestPermission()
|
||||||
{
|
{
|
||||||
HashMap<String, UsbDevice> devices = manager.getDeviceList();
|
Context context = NativeLibrary.sEmulationActivity.get();
|
||||||
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
|
if (context != null)
|
||||||
{
|
{
|
||||||
UsbDevice dev = pair.getValue();
|
HashMap<String, UsbDevice> devices = manager.getDeviceList();
|
||||||
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
|
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
|
||||||
{
|
{
|
||||||
if (!manager.hasPermission(dev))
|
UsbDevice dev = pair.getValue();
|
||||||
|
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
|
||||||
{
|
{
|
||||||
Log.warning("Requesting permission for Wii Remote adapter");
|
if (!manager.hasPermission(dev))
|
||||||
Intent intent = new Intent();
|
{
|
||||||
PendingIntent pend_intent;
|
Log.warning("Requesting permission for Wii Remote adapter");
|
||||||
intent.setClass(NativeLibrary.sEmulationActivity, USBPermService.class);
|
Intent intent = new Intent();
|
||||||
pend_intent = PendingIntent.getService(NativeLibrary.sEmulationActivity, 0, intent, 0);
|
PendingIntent pend_intent;
|
||||||
manager.requestPermission(dev, pend_intent);
|
intent.setClass(context, USBPermService.class);
|
||||||
|
pend_intent = PendingIntent.getService(context, 0, intent, 0);
|
||||||
|
manager.requestPermission(dev, pend_intent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.warning("Cannot request Wiimote adapter permission as EmulationActivity is null.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean QueryAdapter()
|
public static boolean QueryAdapter()
|
||||||
|
|
Loading…
Reference in New Issue