Use weak references for the static Activity in NativeLibrary.

Add in null checks as well.
This commit is contained in:
Mike Harris 2017-10-05 10:11:08 -07:00
parent cde003c5cc
commit 80e1cc56b3
3 changed files with 84 additions and 38 deletions

View File

@ -12,13 +12,15 @@ import android.widget.Toast;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.utils.Log;
import java.lang.ref.WeakReference;
/**
* Class which contains methods that interact
* with the native side of the Dolphin code.
*/
public final class NativeLibrary
{
public static EmulationActivity sEmulationActivity;
public static WeakReference<EmulationActivity> sEmulationActivity = new WeakReference<>(null);
/**
* Button type for use in onTouchEvent
@ -379,31 +381,48 @@ public final class NativeLibrary
public static void displayAlertMsg(final String alert)
{
Log.error("[NativeLibrary] Alert: " + alert);
sEmulationActivity.runOnUiThread(new Runnable()
final EmulationActivity emulationActivity = sEmulationActivity.get();
if (emulationActivity != null)
{
@Override
public void run()
emulationActivity.runOnUiThread(new Runnable()
{
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()
{
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)
{
Log.verbose("[NativeLibrary] Registering EmulationActivity.");
sEmulationActivity = emulationActivity;
sEmulationActivity = new WeakReference<>(emulationActivity);
}
public static void clearEmulationActivity()
{
Log.verbose("[NativeLibrary] Unregistering EmulationActivity.");
sEmulationActivity = null;
sEmulationActivity.clear();
}
}

View File

@ -1,6 +1,8 @@
package org.dolphinemu.dolphinemu.utils;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConfiguration;
import android.hardware.usb.UsbConstants;
@ -15,7 +17,6 @@ import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.services.USBPermService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Java_GCAdapter {
@ -29,22 +30,31 @@ public class Java_GCAdapter {
private static void RequestPermission()
{
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
Context context = NativeLibrary.sEmulationActivity.get();
if (context != null)
{
UsbDevice dev = pair.getValue();
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
HashMap<String, UsbDevice> devices = manager.getDeviceList();
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();
PendingIntent pend_intent;
intent.setClass(NativeLibrary.sEmulationActivity, USBPermService.class);
pend_intent = PendingIntent.getService(NativeLibrary.sEmulationActivity, 0, intent, 0);
manager.requestPermission(dev, pend_intent);
if (!manager.hasPermission(dev))
{
Intent intent = new Intent();
PendingIntent 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()
@ -124,14 +134,22 @@ public class Java_GCAdapter {
}
}
NativeLibrary.sEmulationActivity.runOnUiThread(new Runnable()
final Activity emulationActivity = NativeLibrary.sEmulationActivity.get();
if (emulationActivity != null)
{
@Override
public void run()
emulationActivity.runOnUiThread(new Runnable()
{
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();
}
}

View File

@ -1,6 +1,7 @@
package org.dolphinemu.dolphinemu.utils;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConfiguration;
import android.hardware.usb.UsbDevice;
@ -33,23 +34,31 @@ public class Java_WiimoteAdapter
private static void RequestPermission()
{
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
Context context = NativeLibrary.sEmulationActivity.get();
if (context != null)
{
UsbDevice dev = pair.getValue();
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
HashMap<String, UsbDevice> devices = manager.getDeviceList();
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");
Intent intent = new Intent();
PendingIntent pend_intent;
intent.setClass(NativeLibrary.sEmulationActivity, USBPermService.class);
pend_intent = PendingIntent.getService(NativeLibrary.sEmulationActivity, 0, intent, 0);
manager.requestPermission(dev, pend_intent);
if (!manager.hasPermission(dev))
{
Log.warning("Requesting permission for Wii Remote adapter");
Intent intent = new Intent();
PendingIntent 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()