// Retrieves the normalized coordinates (-1 to 1) for any given (x,y) // value reported by the Android MotionEvent. private boolean getNormalizedCoordinates(MotionEvent motionEvent) { InputDevice device = motionEvent.getDevice(); if (device != null) { InputDevice.MotionRange range = device.getMotionRange(MotionEvent.AXIS_X, motionEvent.getSource()); float x = range.getMax() + 1; range = motionEvent.getDevice().getMotionRange(MotionEvent.AXIS_Y, motionEvent.getSource()); float y = range.getMax() + 1; this.x = (motionEvent.getX() / x * 2.0f - 1.0f); this.y = 1.0f - motionEvent.getY() / y * 2.0f; if (motionEvent.getAction() == MotionEvent.ACTION_SCROLL) { this.z = (motionEvent.getAxisValue(MotionEvent.AXIS_VSCROLL) > 0 ? -1 : 1); } else { this.z = 0; } if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { this.isActive = true; } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { this.isActive = false; } return true; } return false; }
public String printDevice(int id) { String s = ""; s = "ID " + id; InputDevice dev = InputDevice.getDevice(id); if (dev == null) return s; s += " " + dev.getName() + " type " + String.format("0x%08x", dev.getSources()); return s; }
public static Set<Integer> getGameControllerIds() { int[] arrn; HashSet<Integer> hashSet = new HashSet<Integer>(); for (int n : arrn = InputDevice.getDeviceIds()) { int n2 = InputDevice.getDevice((int) n).getSources(); if ((n2 & 1025) != 1025 && (n2 & 16777232) != 16777232) continue; hashSet.add(n); } return hashSet; }
private boolean haveInputDeviceWithVibrator() { final int[] devices = InputDevice.getDeviceIds(); for (int i = 0; i < devices.length; i++) { InputDevice device = InputDevice.getDevice(devices[i]); if (device != null && !device.isVirtual() && device.getVibrator().hasVibrator()) { return true; } } return false; }
@TargetApi(16) public static String getInputDesc(InputDevice input) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { return input.getDescriptor(); } else { List<InputDevice.MotionRange> motions = input.getMotionRanges(); String fakeid = ""; for (InputDevice.MotionRange range : motions) fakeid += range.getAxis(); return fakeid; } }
/** @return an array which may be empty but is never null. */ public static int[] inputGetInputDeviceIds(int sources) { int[] ids = InputDevice.getDeviceIds(); int[] filtered = new int[ids.length]; int used = 0; for (int i = 0; i < ids.length; ++i) { InputDevice device = InputDevice.getDevice(ids[i]); if ((device != null) && ((device.getSources() & sources) != 0)) { filtered[used++] = device.getId(); } } return Arrays.copyOf(filtered, used); }
/** * isExternalKeyboard * * @return boolean */ public boolean isExternalKeyboard() { if (mInputDevice.getSources() == InputDevice.SOURCE_KEYBOARD) { if (isExternal()) { return true; } } return false; }
@Override public boolean onTouchEvent(MotionEvent ev) { if (isFinished()) return true; if (ev.getAction() != MotionEvent.ACTION_UP) return true; if (mDeviceName == null) { int devId = ev.getDeviceId(); InputDevice dev = InputDevice.getDevice(devId); if (dev != null) { mDeviceName = dev.getName(); } } mTargetPoints[mStep].calx = ev.getRawX() * ev.getXPrecision(); mTargetPoints[mStep].caly = ev.getRawY() * ev.getYPrecision(); mStep++; mContext.onCalTouchEvent(ev); return true; }
private void updateHardKeyboards() { mHardKeyboardPreferenceList.clear(); if (getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY) { final int[] devices = InputDevice.getDeviceIds(); for (int i = 0; i < devices.length; i++) { InputDevice device = InputDevice.getDevice(devices[i]); if (device != null && !device.isVirtual() && device.isFullKeyboard()) { final String inputDeviceDescriptor = device.getDescriptor(); final String keyboardLayoutDescriptor = mIm.getCurrentKeyboardLayoutForInputDevice(inputDeviceDescriptor); final KeyboardLayout keyboardLayout = keyboardLayoutDescriptor != null ? mIm.getKeyboardLayout(keyboardLayoutDescriptor) : null; final PreferenceScreen pref = new PreferenceScreen(getActivity(), null); pref.setTitle(device.getName()); if (keyboardLayout != null) { pref.setSummary(keyboardLayout.toString()); } else { pref.setSummary(R.string.keyboard_layout_default_label); } pref.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { showKeyboardLayoutDialog(inputDeviceDescriptor); return true; } }); mHardKeyboardPreferenceList.add(pref); } } } if (!mHardKeyboardPreferenceList.isEmpty()) { for (int i = mHardKeyboardCategory.getPreferenceCount(); i-- > 0; ) { final Preference pref = mHardKeyboardCategory.getPreference(i); if (pref.getOrder() < 1000) { mHardKeyboardCategory.removePreference(pref); } } Collections.sort(mHardKeyboardPreferenceList); final int count = mHardKeyboardPreferenceList.size(); for (int i = 0; i < count; i++) { final Preference pref = mHardKeyboardPreferenceList.get(i); pref.setOrder(i); mHardKeyboardCategory.addPreference(pref); } getPreferenceScreen().addPreference(mHardKeyboardCategory); } else { getPreferenceScreen().removePreference(mHardKeyboardCategory); } }
/** * Loads the key character maps for the keyboard with the specified device id. * * @param deviceId The device id of the keyboard. * @return The associated key character map. * @throws {@link UnavailableException} if the key character map could not be loaded because it * was malformed or the default key character map is missing from the system. */ public static KeyCharacterMap load(int deviceId) { synchronized (sInstances) { KeyCharacterMap map = sInstances.get(deviceId); if (map == null) { String kcm = null; if (deviceId != VIRTUAL_KEYBOARD) { InputDevice device = InputDevice.getDevice(deviceId); if (device != null) { kcm = device.getKeyCharacterMapFile(); } } if (kcm == null || kcm.length() == 0) { kcm = "/system/usr/keychars/Virtual.kcm"; } int ptr = nativeLoad(kcm); // might throw map = new KeyCharacterMap(deviceId, ptr); sInstances.put(deviceId, map); } return map; } }
@Override public void pollInputDevices() { int[] deviceIds = InputDevice.getDeviceIds(); // It helps processing the device ids in reverse order // For example, in the case of the XBox 360 wireless dongle, // so the first controller seen by SDL matches what the receiver // considers to be the first controller for (int i = deviceIds.length - 1; i > -1; i--) { SDLJoystick joystick = getJoystick(deviceIds[i]); if (joystick == null) { joystick = new SDLJoystick(); InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]); if ((joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { joystick.device_id = deviceIds[i]; joystick.name = joystickDevice.getName(); joystick.axes = new ArrayList<InputDevice.MotionRange>(); for (InputDevice.MotionRange range : joystickDevice.getMotionRanges()) { if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { joystick.axes.add(range); } } mJoysticks.add(joystick); SDLActivity.nativeAddJoystick( joystick.device_id, joystick.name, 0, -1, joystick.axes.size(), 0, 0); } } } /* Check removed devices */ ArrayList<Integer> removedDevices = new ArrayList<Integer>(); for (int i = 0; i < mJoysticks.size(); i++) { int device_id = mJoysticks.get(i).device_id; int j; for (j = 0; j < deviceIds.length; j++) { if (device_id == deviceIds[j]) break; } if (j == deviceIds.length) { removedDevices.add(device_id); } } for (int i = 0; i < removedDevices.size(); i++) { int device_id = removedDevices.get(i); SDLActivity.nativeRemoveJoystick(device_id); for (int j = 0; j < mJoysticks.size(); j++) { if (mJoysticks.get(j).device_id == device_id) { mJoysticks.remove(j); break; } } } }
/** * constractor * * @param InputDevice device */ public MyInputDevice(InputDevice device) { mInputDevice = device; mClass = device.getClass(); }
private void logMotionEvent(MotionEvent event, boolean gen) { String s = gen ? "GenericMotionEvent from " : "TouchEvent from "; s += printDevice(event.getDeviceId()) + " source " + printSource(event.getSource()); int pointerCount = event.getPointerCount(); String action; switch (event.getActionMasked()) { case MotionEvent.ACTION_CANCEL: action = "cancel"; break; case MotionEvent.ACTION_DOWN: action = "down"; break; case MotionEvent.ACTION_HOVER_ENTER: action = "hover_enter"; break; case MotionEvent.ACTION_HOVER_EXIT: action = "hover_exit"; break; case MotionEvent.ACTION_HOVER_MOVE: action = "hover_move"; break; case MotionEvent.ACTION_MOVE: action = "move"; break; case MotionEvent.ACTION_OUTSIDE: action = "outside"; break; case MotionEvent.ACTION_POINTER_DOWN: action = "pointer_down"; break; case MotionEvent.ACTION_POINTER_UP: action = "pointer_up"; break; case MotionEvent.ACTION_UP: action = "up"; break; case MotionEvent.ACTION_SCROLL: action = "scroll"; break; default: action = "" + event.getAction(); break; } s += ", action " + action + " pointer count " + pointerCount + " buttons " + event.getButtonState() + " RawX " + event.getRawX() + " RawY " + event.getRawY() + " MetaState " + event.getMetaState() + " Flags " + event.getFlags() + "\n"; for (int p = 0; p < pointerCount; p++) { int ptrId = event.getPointerId(p); s += "Pointer id " + ptrId + " X " + event.getX(p) + " Y " + event.getY(p) + " pressure " + event.getPressure(p) + " size " + event.getSize(p) + " orientation " + event.getOrientation(p) + // (float)Math.toDegrees(event.getOrientation(p)) + " TouchMajor " + event.getTouchMajor(p) + " TouchMinor " + event.getTouchMinor(p) + " ToolMajor " + event.getToolMajor(p) + " ToolMinor " + event.getToolMinor(p) + "\n"; for (int i = 0; i < 255; i++) { if (event.getAxisValue(i, p) != 0.0) { s += "Axis " + i + " " + event.axisToString(i) + ": " + event.getAxisValue(i, p); InputDevice device = InputDevice.getDevice(event.getDeviceId()); if (device != null) { InputDevice.MotionRange range = device.getMotionRange(i); if (range != null) s += " range " + range.getMin() + " to " + range.getMax(); } s += "\n"; } } } pushText(s); }
public InputDevice createFromParcel(Parcel in) { InputDevice result = new InputDevice(); result.readFromParcel(in); return result; }