Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Android #35

Merged
merged 15 commits into from
Jun 18, 2024

Conversation

markvideon
Copy link
Collaborator

@markvideon markvideon commented Jun 8, 2024

Addressing #17.

Note that the example project as-is will fail due to the published release of gamepads 0.1.1 not depending on gamepads_android. Manually adding gamepads_android: ^0.1.1 as a dependency to the example project will allow you to test everything links as expected.

Edit: Also note that there are required integration steps for consuming apps on Android. Essentially this implementation relies on MainActivity forwarding [KeyEvent]s and [MotionEvent]s to the plug-in class.

@markvideon markvideon requested a review from spydon June 8, 2024 02:02
@spydon spydon requested review from luanpotter and erickzanardo June 8, 2024 08:34
README.md Outdated Show resolved Hide resolved
Copy link
Member

@spydon spydon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm, good job!
Just some very minor nits.

packages/gamepads_android/CHANGELOG.md Outdated Show resolved Hide resolved
packages/gamepads_android/README.md Show resolved Hide resolved
packages/gamepads_android/pubspec.yaml Show resolved Hide resolved
packages/gamepads_android/pubspec.yaml Outdated Show resolved Hide resolved
README.md Outdated
Comment on lines 131 to 134
override fun isGamepadsInputDevice(device: InputDevice): Boolean {
return device.sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
|| device.sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would users need to override this method from GamepadsCompatibleActivity?
can't we just provide the impl for this?

I would prefer to minimize any code that users have to add manually to use the libary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a similar line of thought, I wonder if we can provide a GamepadsCompatibleActivityAdapter, that implements all these required methods on behalf of the user just requiring the minimum hooks to be added:

  • wire in registerKeyEventHandler and registerMotionEventHandler
  • access to getSystemService(INPUT_SERVICE)
    it appears that everything else is boilerplate that would be the same for all (or most) users using these 3 variables and thus could be hidden away by us
    but I might be missing something, I am only looking at the code on github. I can try to pull on IJ later to take a look

Copy link
Member

@erickzanardo erickzanardo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. I just wonde why make this a standalone plugin instead of adding it under the gamepads one that already exists, in a federated structure

@markvideon
Copy link
Collaborator Author

Overall looks good to me. I just wonde why make this a standalone plugin instead of adding it under the gamepads one that already exists, in a federated structure

It's being integrated, I'll just pass it on

@renancaraujo
Copy link
Member

Didn't look much into the code, but testing on a Samsung s21 ultra and a Sony xperia 1 V, both with android 14,it seems like events are not being registered and list gamepads never resolves?

Am i missing anything when setting it up?
(added gamepads_android as direct dependency too).

@markvideon
Copy link
Collaborator Author

Didn't look much into the code, but testing on a Samsung s21 ultra and a Sony xperia 1 V, both with android 14,it seems like events are not being registered and list gamepads never resolves?

Am i missing anything when setting it up?

(added gamepads_android as direct dependency too).

Did you perform the steps in the updated README?

markvideon and others added 6 commits June 16, 2024 13:44
…on/gamepads_android/EventListener.kt

Co-authored-by: Luan Nico <[email protected]>
…on/gamepads_android/EventListener.kt

Co-authored-by: Luan Nico <[email protected]>
…on/gamepads_android/GamepadsAndroidPlugin.kt

Co-authored-by: Luan Nico <[email protected]>
…nst, adjustment to lambda definition for readability as per PR feedback
@markvideon
Copy link
Collaborator Author

markvideon commented Jun 16, 2024

Didn't look much into the code, but testing on a Samsung s21 ultra and a Sony xperia 1 V, both with android 14,it seems like events are not being registered and list gamepads never resolves?

Am i missing anything when setting it up? (added gamepads_android as direct dependency too).

Now that the README on the feature branch has been altered, you can either point to the head of this feature branch and use those README instructions, or you can follow these:

Sample integration for gamepads_android 0.1.1
package [YOUR_PACKAGE_NAME]

import android.hardware.input.InputManager
import android.os.Handler
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import io.flutter.embedding.android.FlutterActivity
import dev.markvideon.gamepads_android.GamepadsCompatibleActivity

class MainActivity: FlutterActivity(), GamepadsCompatibleActivity {
    var keyListener: ((KeyEvent) -> Boolean)? = null
    var motionListener: ((MotionEvent) -> Boolean)? = null

    override fun dispatchGenericMotionEvent(motionEvent: MotionEvent): Boolean {
        motionListener?.invoke(motionEvent)
        return true
    }

    override fun dispatchKeyEvent(keyEvent: KeyEvent): Boolean {
        keyListener?.invoke(keyEvent)
        return true
    }

    override fun isGamepadsInputDevice(device: InputDevice): Boolean {
        return device.sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
            || device.sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
    }

    override fun registerInputDeviceListener(listener: InputManager.InputDeviceListener, handler: Handler?) {
        val inputManager = getSystemService(INPUT_SERVICE) as InputManager
        inputManager.registerInputDeviceListener(listener, null)
    }

    override fun registerKeyEventHandler(handler: (KeyEvent) -> Boolean) {
        keyListener = handler
    }

    override fun registerMotionEventHandler(handler: (MotionEvent) -> Boolean) {
        motionListener = handler
    }
}

@markvideon markvideon merged commit 6996109 into flame-engine:main Jun 18, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants