Move key handling back to the Activity, otherwise we don't seem to receive onKeyUp() for the Android back button

This commit is contained in:
Aaron Culliney 2015-12-20 12:11:12 -08:00
parent 2f7f7bd359
commit 22b1bc9ad8
3 changed files with 45 additions and 46 deletions

View File

@ -22,6 +22,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
@ -73,6 +74,10 @@ public class Apple2Activity extends Activity {
private native void nativeOnCreate(String dataDir, int sampleRate, int monoBufferSize, int stereoBufferSize);
private static native void nativeOnKeyDown(int keyCode, int metaState);
private static native void nativeOnKeyUp(int keyCode, int metaState);
private native void nativeSaveState(String path);
private native String nativeLoadState(String path);
@ -265,6 +270,44 @@ public class Apple2Activity extends Activity {
mPausing.set(false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Apple2Activity.isNativeBarfed()) {
return super.onKeyDown(keyCode, event);
}
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
return super.onKeyDown(keyCode, event);
}
nativeOnKeyDown(keyCode, event.getMetaState());
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (Apple2Activity.isNativeBarfed()) {
return super.onKeyUp(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
Apple2MenuView apple2MenuView = peekApple2View();
if (apple2MenuView == null) {
showMainMenu();
} else {
apple2MenuView.dismiss();
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
showMainMenu();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
return super.onKeyUp(keyCode, event);
}
nativeOnKeyUp(keyCode, event.getMetaState());
return true;
}
public void showMainMenu() {
if (mMainMenu != null) {
if (!(mSettingsMenu.isShowing() || mDisksMenu.isShowing())) {

View File

@ -24,8 +24,6 @@ import android.opengl.GLSurfaceView;
import android.os.Build;
import android.util.Log;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
@ -77,10 +75,6 @@ class Apple2View extends GLSurfaceView implements InputManagerCompat.InputDevice
private static native void nativeOnJoystickMove(int x, int y);
private static native void nativeOnKeyDown(int keyCode, int metaState);
private static native void nativeOnKeyUp(int keyCode, int metaState);
public static native long nativeOnTouch(int action, int pointerCount, int pointerIndex, float[] xCoords, float[] yCoords);
@ -487,44 +481,6 @@ class Apple2View extends GLSurfaceView implements InputManagerCompat.InputDevice
return 0;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Apple2Activity.isNativeBarfed()) {
return super.onKeyDown(keyCode, event);
}
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
return super.onKeyDown(keyCode, event);
}
nativeOnKeyDown(keyCode, event.getMetaState());
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (Apple2Activity.isNativeBarfed()) {
return super.onKeyUp(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
Apple2MenuView apple2MenuView = mActivity.peekApple2View();
if (apple2MenuView == null) {
mActivity.showMainMenu();
} else {
apple2MenuView.dismiss();
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
mActivity.showMainMenu();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
return super.onKeyUp(keyCode, event);
}
nativeOnKeyUp(keyCode, event.getMetaState());
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
do {

View File

@ -281,14 +281,14 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnQuit(JNIEnv *env, jobject
#endif
}
void Java_org_deadc0de_apple2ix_Apple2View_nativeOnKeyDown(JNIEnv *env, jclass cls, jint keyCode, jint metaState) {
void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnKeyDown(JNIEnv *env, jclass cls, jint keyCode, jint metaState) {
if (UNLIKELY(appState != APP_RUNNING)) {
return;
}
android_keycode_to_emulator(keyCode, metaState, true);
}
void Java_org_deadc0de_apple2ix_Apple2View_nativeOnKeyUp(JNIEnv *env, jclass cls, jint keyCode, jint metaState) {
void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnKeyUp(JNIEnv *env, jclass cls, jint keyCode, jint metaState) {
if (UNLIKELY(appState != APP_RUNNING)) {
return;
}