From 88b81a6652df5e2365ea1118f7309adcad3d5c21 Mon Sep 17 00:00:00 2001 From: Ender's Games Date: Wed, 12 Sep 2018 22:16:25 -0400 Subject: [PATCH] Android: Improve stability of gamepad detection --- .../com/reicast/emulator/GL2JNIActivity.java | 8 ++++---- .../com/reicast/emulator/GL2JNINative.java | 18 ++++++++---------- .../com/reicast/emulator/MainActivity.java | 4 +--- .../reicast/emulator/debug/GenerateLogs.java | 4 ---- .../com/reicast/emulator/periph/Gamepad.java | 8 ++------ 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNIActivity.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNIActivity.java index d51625afe..f86769dd0 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNIActivity.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNIActivity.java @@ -149,11 +149,13 @@ public class GL2JNIActivity extends Activity { pad.deviceId_deviceDescriptor.put(joy, descriptor); } - for (int joy :joys) { + boolean detected = false; + for (int joy : joys) { Integer playerNum = pad.deviceDescriptor_PlayerNum .get(pad.deviceId_deviceDescriptor.get(joy)); if (playerNum != null) { + detected = true; String id = pad.portId[playerNum]; pad.custom[playerNum] = prefs.getBoolean(Gamepad.pref_js_modified + id, false); pad.compat[playerNum] = prefs.getBoolean(Gamepad.pref_js_compat + id, false); @@ -187,11 +189,9 @@ public class GL2JNIActivity extends Activity { pad.getCompatibilityMap(playerNum, id, prefs); } pad.initJoyStickLayout(playerNum); - } else { - pad.runCompatibilityMode(joy, prefs); } } - if (joys.length == 0) { + if (joys.length == 0 || !detected) { pad.fullCompatibilityMode(prefs); } diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNINative.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNINative.java index b00853258..fdf75038d 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNINative.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/GL2JNINative.java @@ -144,25 +144,22 @@ public class GL2JNINative extends NativeActivity { pad.deviceId_deviceDescriptor.put(joy, descriptor); } + boolean detected = false; for (int joy : joys) { Integer playerNum = pad.deviceDescriptor_PlayerNum .get(pad.deviceId_deviceDescriptor.get(joy)); if (playerNum != null) { + detected = true; String id = pad.portId[playerNum]; pad.custom[playerNum] = prefs.getBoolean(Gamepad.pref_js_modified + id, false); pad.compat[playerNum] = prefs.getBoolean(Gamepad.pref_js_compat + id, false); pad.joystick[playerNum] = prefs.getBoolean(Gamepad.pref_js_merged + id, false); - if (InputDevice.getDevice(joy).getName() - .contains(Gamepad.controllers_gamekey)) { -// if (pad.custom[playerNum]) { -// setCustomMapping(id, playerNum); -// } else { -// pad.map[playerNum] = pad.getConsoleController(); -// } + if (InputDevice.getDevice(joy).getName().contains(Gamepad.controllers_gamekey)) { new Handler().post(new Runnable() { public void run() { - Toast.makeText(getApplicationContext(), R.string.controller_unavailable, + Toast.makeText(getApplicationContext(), + R.string.controller_unavailable, Toast.LENGTH_SHORT).show(); finish(); } @@ -190,10 +187,11 @@ public class GL2JNINative extends NativeActivity { } pad.initJoyStickLayout(playerNum); pad.playerNumX.put(joy, playerNum); - } else { - pad.runCompatibilityMode(joy, prefs); } } + if (joys.length == 0 || !detected) { + pad.fullCompatibilityMode(prefs); + } app.loadConfigurationPrefs(); diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/MainActivity.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/MainActivity.java index 361590afc..3e4c2f432 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/MainActivity.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/MainActivity.java @@ -53,8 +53,6 @@ public class MainActivity extends AppCompatActivity implements private SharedPreferences mPrefs; private boolean hasAndroidMarket = false; - private UncaughtExceptionHandler mUEHandler; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -85,7 +83,7 @@ public class MainActivity extends AppCompatActivity implements displayLogOutput(prior_error); mPrefs.edit().remove("prior_error").apply(); } else { - mUEHandler = new Thread.UncaughtExceptionHandler() { + UncaughtExceptionHandler mUEHandler = new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable error) { if (error != null) { StringBuilder output = new StringBuilder(); diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/debug/GenerateLogs.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/debug/GenerateLogs.java index 417f01501..2fa204d07 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/debug/GenerateLogs.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/debug/GenerateLogs.java @@ -170,8 +170,6 @@ public class GenerateLogs extends AsyncTask { log.append(separator); } reader.close(); - mLogcatProc = null; - reader = null; File memory = new File(mContext.get().getFilesDir(), "mem_alloc.txt"); if (memory.exists()) { log.append(separator); @@ -186,9 +184,7 @@ public class GenerateLogs extends AsyncTask { log.append(separator); } fis.close(); - fis = null; reader.close(); - reader = null; } BufferedWriter writer = new BufferedWriter(new FileWriter(logFile)); writer.write(log.toString()); diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/periph/Gamepad.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/periph/Gamepad.java index 23df12e82..08d348cd9 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/periph/Gamepad.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/periph/Gamepad.java @@ -202,18 +202,14 @@ public class Gamepad { } } - public void runCompatibilityMode(int joy, SharedPreferences prefs) { + public void fullCompatibilityMode(SharedPreferences prefs) { + for (int joy = 0; joy < 4; joy++) { if (compat[joy]) { String id = portId[joy]; joystick[joy] = prefs.getBoolean(Gamepad.pref_js_merged + id, false); getCompatibilityMap(joy, portId[joy], prefs); initJoyStickLayout(joy); } - } - - public void fullCompatibilityMode(SharedPreferences prefs) { - for (int n = 0; n < 4; n++) { - runCompatibilityMode(n, prefs); } }