1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/util/GamepadUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.util; 1.10 + 1.11 +import android.os.Build; 1.12 +import android.view.InputDevice; 1.13 +import android.view.KeyCharacterMap; 1.14 +import android.view.KeyEvent; 1.15 +import android.view.MotionEvent; 1.16 +import android.view.View; 1.17 +import android.widget.AdapterView; 1.18 +import android.widget.ListView; 1.19 + 1.20 +public final class GamepadUtils { 1.21 + private static final int SONY_XPERIA_GAMEPAD_DEVICE_ID = 196611; 1.22 + 1.23 + private static View.OnKeyListener sClickDispatcher; 1.24 + private static float sDeadZoneThresholdOverride = 1e-2f; 1.25 + 1.26 + private GamepadUtils() { 1.27 + } 1.28 + 1.29 + private static boolean isGamepadKey(KeyEvent event) { 1.30 + if (Build.VERSION.SDK_INT >= 9) { 1.31 + return (event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD; 1.32 + } 1.33 + return false; 1.34 + } 1.35 + 1.36 + public static boolean isActionKey(KeyEvent event) { 1.37 + return (isGamepadKey(event) && (event.getKeyCode() == KeyEvent.KEYCODE_BUTTON_A)); 1.38 + } 1.39 + 1.40 + public static boolean isActionKeyDown(KeyEvent event) { 1.41 + return isActionKey(event) && event.getAction() == KeyEvent.ACTION_DOWN; 1.42 + } 1.43 + 1.44 + public static boolean isBackKey(KeyEvent event) { 1.45 + return (isGamepadKey(event) && (event.getKeyCode() == KeyEvent.KEYCODE_BUTTON_B)); 1.46 + } 1.47 + 1.48 + public static void overrideDeadZoneThreshold(float threshold) { 1.49 + sDeadZoneThresholdOverride = threshold; 1.50 + } 1.51 + 1.52 + public static boolean isValueInDeadZone(MotionEvent event, int axis) { 1.53 + if (Build.VERSION.SDK_INT < 9) { 1.54 + return false; 1.55 + } 1.56 + 1.57 + float threshold; 1.58 + if (sDeadZoneThresholdOverride >= 0) { 1.59 + threshold = sDeadZoneThresholdOverride; 1.60 + } else { 1.61 + InputDevice.MotionRange range = event.getDevice().getMotionRange(axis); 1.62 + threshold = range.getFlat() + range.getFuzz(); 1.63 + } 1.64 + float value = event.getAxisValue(axis); 1.65 + return (Math.abs(value) < threshold); 1.66 + } 1.67 + 1.68 + public static boolean isPanningControl(MotionEvent event) { 1.69 + if (Build.VERSION.SDK_INT < 12) { 1.70 + return false; 1.71 + } 1.72 + if ((event.getSource() & InputDevice.SOURCE_CLASS_MASK) != InputDevice.SOURCE_CLASS_JOYSTICK) { 1.73 + return false; 1.74 + } 1.75 + if (isValueInDeadZone(event, MotionEvent.AXIS_X) 1.76 + && isValueInDeadZone(event, MotionEvent.AXIS_Y) 1.77 + && isValueInDeadZone(event, MotionEvent.AXIS_Z) 1.78 + && isValueInDeadZone(event, MotionEvent.AXIS_RZ)) { 1.79 + return false; 1.80 + } 1.81 + return true; 1.82 + } 1.83 + 1.84 + public static View.OnKeyListener getClickDispatcher() { 1.85 + if (sClickDispatcher == null) { 1.86 + sClickDispatcher = new View.OnKeyListener() { 1.87 + @Override 1.88 + public boolean onKey(View v, int keyCode, KeyEvent event) { 1.89 + if (isActionKeyDown(event)) { 1.90 + return v.performClick(); 1.91 + } 1.92 + return false; 1.93 + } 1.94 + }; 1.95 + } 1.96 + return sClickDispatcher; 1.97 + } 1.98 + 1.99 + public static KeyEvent translateSonyXperiaGamepadKeys(int keyCode, KeyEvent event) { 1.100 + // The cross and circle button mappings may be swapped in the different regions so 1.101 + // determine if they are swapped so the proper key codes can be mapped to the keys 1.102 + boolean areKeysSwapped = areSonyXperiaGamepadKeysSwapped(); 1.103 + 1.104 + // If a Sony Xperia, remap the cross and circle buttons to buttons 1.105 + // A and B for the gamepad API 1.106 + switch (keyCode) { 1.107 + case KeyEvent.KEYCODE_BACK: 1.108 + keyCode = (areKeysSwapped ? KeyEvent.KEYCODE_BUTTON_A : KeyEvent.KEYCODE_BUTTON_B); 1.109 + break; 1.110 + 1.111 + case KeyEvent.KEYCODE_DPAD_CENTER: 1.112 + keyCode = (areKeysSwapped ? KeyEvent.KEYCODE_BUTTON_B : KeyEvent.KEYCODE_BUTTON_A); 1.113 + break; 1.114 + 1.115 + default: 1.116 + return event; 1.117 + } 1.118 + 1.119 + return new KeyEvent(event.getAction(), keyCode); 1.120 + } 1.121 + 1.122 + public static boolean isSonyXperiaGamepadKeyEvent(KeyEvent event) { 1.123 + return (event.getDeviceId() == SONY_XPERIA_GAMEPAD_DEVICE_ID && 1.124 + "Sony Ericsson".equals(Build.MANUFACTURER) && 1.125 + ("R800".equals(Build.MODEL) || "R800i".equals(Build.MODEL))); 1.126 + } 1.127 + 1.128 + private static boolean areSonyXperiaGamepadKeysSwapped() { 1.129 + // The cross and circle buttons on Sony Xperia phones are swapped 1.130 + // in different regions 1.131 + // http://developer.sonymobile.com/2011/02/13/xperia-play-game-keys/ 1.132 + final char DEFAULT_O_BUTTON_LABEL = 0x25CB; 1.133 + 1.134 + boolean swapped = false; 1.135 + int[] deviceIds = InputDevice.getDeviceIds(); 1.136 + 1.137 + for (int i= 0; deviceIds != null && i < deviceIds.length; i++) { 1.138 + KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(deviceIds[i]); 1.139 + if (keyCharacterMap != null && DEFAULT_O_BUTTON_LABEL == 1.140 + keyCharacterMap.getDisplayLabel(KeyEvent.KEYCODE_DPAD_CENTER)) { 1.141 + swapped = true; 1.142 + break; 1.143 + } 1.144 + } 1.145 + return swapped; 1.146 + } 1.147 +}