Java Code Examples for android.view.InputDevice#SOURCE_JOYSTICK

The following examples show how to use android.view.InputDevice#SOURCE_JOYSTICK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioPlayerActivity.java    From VCL-Android with Apache License 2.0 6 votes vote down vote up
public boolean dispatchGenericMotionEvent(MotionEvent event){
    //Check for a joystick event
    if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) !=
            InputDevice.SOURCE_JOYSTICK ||
            event.getAction() != MotionEvent.ACTION_MOVE)
        return false;

    InputDevice inputDevice = event.getDevice();

    float dpadx = event.getAxisValue(MotionEvent.AXIS_HAT_X);
    float dpady = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
    if (inputDevice == null || Math.abs(dpadx) == 1.0f || Math.abs(dpady) == 1.0f)
        return false;

    float x = AndroidDevices.getCenteredAxis(event, inputDevice,
            MotionEvent.AXIS_X);

    if (System.currentTimeMillis() - mLastMove > JOYSTICK_INPUT_DELAY){
        if (Math.abs(x) > 0.3){
            seek(x > 0.0f ? 10000 : -10000);
            mLastMove = System.currentTimeMillis();
            return true;
        }
    }
    return true;
}
 
Example 2
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 6 votes vote down vote up
/**
 * Check for any game controllers that are connected already.
 */
private void checkGameControllers() {
    Log.d(TAG, "checkGameControllers");
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or
        // both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                || ((sources & InputDevice.SOURCE_JOYSTICK)
                    == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!mConnectedDevices.contains(deviceId)) {
                mConnectedDevices.add(deviceId);
                if (mCurrentDeviceId == -1) {
                    mCurrentDeviceId = deviceId;
                    mControllerView.setCurrentControllerNumber(dev.getControllerNumber());
                    mControllerView.invalidate();
                }
            }
        }
    }
}
 
Example 3
Source File: AndroidInputHandler14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
    public boolean onGenericMotion(View view, MotionEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

        int source = event.getSource();
//        logger.log(Level.INFO, "onGenericMotion source: {0}", source);

        boolean isJoystick =
                ((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);

        if (isJoystick && joyInput != null) {
//            logger.log(Level.INFO, "onGenericMotion source: {0}, isJoystick: {1}",
//                    new Object[]{source, isJoystick});
            // send the event to the touch processor
            consumed = consumed || ((AndroidJoyInput14)joyInput).onGenericMotion(event);
        }

        return consumed;
    }
 
Example 4
Source File: GameView.java    From bluetooth with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    mInputManager.onGenericMotionEvent(event);

    // Check that the event came from a joystick or gamepad since a generic
    // motion event could be almost anything. API level 18 adds the useful
    // event.isFromSource() helper function.
    int eventSource = event.getSource();
    if ((((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
            ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK))
            && event.getAction() == MotionEvent.ACTION_MOVE) {
        int id = event.getDeviceId();
        if (-1 != id) {
            Ship curShip = getShipForId(id);
            if (curShip.onGenericMotionEvent(event)) {
                return true;
            }
        }
    }
    return super.onGenericMotionEvent(event);
}
 
Example 5
Source File: AndroidView.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
	if (game.getCurrentScreen() == null) {
		return true;
	}
	if (isController(event.getSource())) {
		if (Dpad.isDpadDevice(event)) {
			if (game.getCurrentScreen() != null) {
				game.getCurrentScreen().processDPad(dpad.getDirectionPressed(event));
			}
		} 
		// Check that the event came from a game controller
        if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) ==
                InputDevice.SOURCE_JOYSTICK &&
                event.getAction() == MotionEvent.ACTION_MOVE) {

            // Process all historical movement samples in the batch
            final int historySize = event.getHistorySize();

            // Process the movements starting from the
            // earliest historical position in the batch
            for (int i = 0; i < historySize; i++) {
                // Process the event at historical position i
                processJoystickInput(game.getCurrentScreen(), event, i);
            }

            // Process the current movement sample in the batch (position -1)
            processJoystickInput(game.getCurrentScreen(), event, -1);
        }
	}
	return super.onGenericMotionEvent(event);
}
 
Example 6
Source File: FullscreenActivity.java    From androidtv-VisualGameController with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to determine if input device is a gamepad.
 * 
 * @param device
 * @return
 */
private boolean isGamepad(InputDevice device) {
    if ((device.getSources() &
            InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
            || (device.getSources() &
            InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
        return true;
    }
    return false;
}
 
Example 7
Source File: AndroidInputHandler14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

//        logger.log(Level.INFO, "onKey keyCode: {0}, action: {1}, event: {2}",
//                new Object[]{KeyEvent.keyCodeToString(keyCode), event.getAction(), event});
        int source = event.getSource();
//        logger.log(Level.INFO, "onKey source: {0}", source);

        boolean isTouch =
                ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) ||
                ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD);
        boolean isJoystick =
                ((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);
        boolean isUnknown =
                (source & android.view.InputDevice.SOURCE_UNKNOWN) == android.view.InputDevice.SOURCE_UNKNOWN;

        if (touchInput != null && (isTouch || (isUnknown && this.touchInput.isSimulateKeyboard()))) {
//            logger.log(Level.INFO, "onKey source: {0}, isTouch: {1}",
//                    new Object[]{source, isTouch});
            consumed = touchInput.onKey(event);
        }
        if (isJoystick && joyInput != null) {
//            logger.log(Level.INFO, "onKey source: {0}, isJoystick: {1}",
//                    new Object[]{source, isJoystick});
            // use inclusive OR to make sure the onKey method is called.
            consumed = consumed | ((AndroidJoyInput14)joyInput).onKey(event);
        }

        return consumed;

    }
 
Example 8
Source File: MainActivity.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
public ArrayList getGameControllerIds() {
    ArrayList gameControllerDeviceIds = new ArrayList();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
            || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            name.setText(dev.getName());
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
            }
            //possible both maybe true.
            if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                isGamePad = true;
            if ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)
                isJoyStick = true;
            logger.append("GamePad: " + isGamePad + "\n");
            logger.append("JoyStick: " + isJoyStick + "\n");
        }

    }
    return gameControllerDeviceIds;
}
 
Example 9
Source File: GameView.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
void findControllersAndAttachShips() {
    int[] deviceIds = mInputManager.getInputDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = mInputManager.getInputDevice(deviceId);
        int sources = dev.getSources();
        // if the device is a gamepad/joystick, create a ship to represent it
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // if the device has a gamepad or joystick
            getShipForId(deviceId);
        }
    }
}
 
Example 10
Source File: VideoPlayerActivity.java    From VCL-Android with Apache License 2.0 4 votes vote down vote up
@TargetApi(12) //only active for Android 3.1+
public boolean dispatchGenericMotionEvent(MotionEvent event){
    if (mIsLoading)
        return  false;
    //Check for a joystick event
    if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) !=
            InputDevice.SOURCE_JOYSTICK ||
            event.getAction() != MotionEvent.ACTION_MOVE)
        return false;

    InputDevice mInputDevice = event.getDevice();

    float dpadx = event.getAxisValue(MotionEvent.AXIS_HAT_X);
    float dpady = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
    if (mInputDevice == null || Math.abs(dpadx) == 1.0f || Math.abs(dpady) == 1.0f)
        return false;

    float x = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_X);
    float y = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_Y);
    float rz = AndroidDevices.getCenteredAxis(event, mInputDevice,
            MotionEvent.AXIS_RZ);

    if (System.currentTimeMillis() - mLastMove > JOYSTICK_INPUT_DELAY){
        if (Math.abs(x) > 0.3){
            if (BuildConfig.tv) {
                navigateDvdMenu(x > 0.0f ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT);
            } else
                seekDelta(x > 0.0f ? 10000 : -10000);
        } else if (Math.abs(y) > 0.3){
            if (BuildConfig.tv)
                navigateDvdMenu(x > 0.0f ? KeyEvent.KEYCODE_DPAD_UP : KeyEvent.KEYCODE_DPAD_DOWN);
            else {
                if (mIsFirstBrightnessGesture)
                    initBrightnessTouch();
                changeBrightness(-y / 10f);
            }
        } else if (Math.abs(rz) > 0.3){
            mVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int delta = -(int) ((rz / 7) * mAudioMax);
            int vol = (int) Math.min(Math.max(mVol + delta, 0), mAudioMax);
            setAudioVolume(vol);
        }
        mLastMove = System.currentTimeMillis();
    }
    return true;
}
 
Example 11
Source File: AliteIntro.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private boolean isController(int sourceId) {
	return (sourceId & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
		   (sourceId & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD ||
		   (sourceId & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
}
 
Example 12
Source File: AndroidView.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private boolean isController(int sourceId) {
	return (sourceId & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
		   (sourceId & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD ||
		   (sourceId & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
}
 
Example 13
Source File: GamepadList.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private static boolean isGamepadDevice(InputDevice inputDevice) {
    if (inputDevice == null) return false;
    return ((inputDevice.getSources() & InputDevice.SOURCE_JOYSTICK)
            == InputDevice.SOURCE_JOYSTICK);
}
 
Example 14
Source File: GamepadList.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * @return True if the motion event corresponds to a gamepad event.
 */
public static boolean isGamepadEvent(MotionEvent event) {
    return ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);
}
 
Example 15
Source File: AndroidJoystickJoyInput14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Joystick> loadJoysticks(int joyId, InputManager inputManager) {
    logger.log(Level.INFO, "loading Joystick devices");
    ArrayList<Joystick> joysticks = new ArrayList<Joystick>();
    joysticks.clear();
    joystickIndex.clear();

    ArrayList<Integer> gameControllerDeviceIds = new ArrayList<>();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();
        logger.log(Level.FINE, "deviceId[{0}] sources: {1}", new Object[]{deviceId, sources});

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
                logger.log(Level.FINE, "Attempting to create joystick for device: {0}", dev);
                // Create an AndroidJoystick and store the InputDevice so we
                // can later correspond the input from the InputDevice to the
                // appropriate jME Joystick event
                AndroidJoystick joystick = new AndroidJoystick(inputManager,
                                                            joyInput,
                                                            dev,
                                                            joyId+joysticks.size(),
                                                            dev.getName());
                joystickIndex.put(deviceId, joystick);
                joysticks.add(joystick);

                // Each analog input is reported as a MotionRange
                // The axis number corresponds to the type of axis
                // The AndroidJoystick.addAxis(MotionRange) converts the axis
                // type reported by Android into the jME Joystick axis
                List<MotionRange> motionRanges = dev.getMotionRanges();
                for (MotionRange motionRange: motionRanges) {
                    logger.log(Level.INFO, "motion range: {0}", motionRange.toString());
                    logger.log(Level.INFO, "axis: {0}", motionRange.getAxis());
                    JoystickAxis axis = joystick.addAxis(motionRange);
                    logger.log(Level.INFO, "added axis: {0}", axis);
                }

                // InputDevice has a method for determining if a keyCode is
                // supported (InputDevice  public boolean[] hasKeys (int... keys)).
                // But this method wasn't added until rev 19 (Android 4.4)
                // Therefore, we only can query the entire device and see if
                // any InputDevice supports the keyCode.  This may result in
                // buttons being configured that don't exist on the specific
                // device, but I haven't found a better way yet.
                for (int keyCode: AndroidGamepadButtons) {
                    logger.log(Level.INFO, "button[{0}]: {1}",
                            new Object[]{keyCode, KeyCharacterMap.deviceHasKey(keyCode)});
                    if (KeyCharacterMap.deviceHasKey(keyCode)) {
                        // add button even though we aren't sure if the button
                        // actually exists on this InputDevice
                        logger.log(Level.INFO, "button[{0}] exists somewhere", keyCode);
                        JoystickButton button = joystick.addButton(keyCode);
                        logger.log(Level.INFO, "added button: {0}", button);
                    }
                }

            }
        }
    }


    loaded = true;
    return joysticks;
}