MouseDevice.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. package com.amazon.lumberyard.input;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.hardware.input.InputManager;
  12. import android.view.InputDevice;
  13. import java.util.HashSet;
  14. import java.util.Set;
  15. public class MouseDevice
  16. implements InputManager.InputDeviceListener
  17. {
  18. public native void OnMouseConnected();
  19. public native void OnMouseDisconnected();
  20. public MouseDevice(Activity activity)
  21. {
  22. m_inputManager = (InputManager)activity.getSystemService(Context.INPUT_SERVICE);
  23. int[] devices = m_inputManager.getInputDeviceIds();
  24. for (int deviceId : devices)
  25. {
  26. if (IsMouseDevice(deviceId))
  27. {
  28. m_mouseDeviceIds.add(deviceId);
  29. }
  30. }
  31. final InputManager.InputDeviceListener listener = this;
  32. activity.runOnUiThread(new Runnable() {
  33. @Override
  34. public void run() {
  35. // run the registration on the main thread to use it's looper as the handler
  36. // instead of creating one specifically for listening to mouse [dis]connections
  37. m_inputManager.registerInputDeviceListener(listener, null);
  38. }
  39. });
  40. }
  41. @Override
  42. public void onInputDeviceAdded(int deviceId)
  43. {
  44. if (IsMouseDevice(deviceId))
  45. {
  46. m_mouseDeviceIds.add(deviceId);
  47. // only inform the native code if we change from having no mice connected, extra
  48. // are effectively ignored and folded into one "master" device
  49. if (m_mouseDeviceIds.size() == 1)
  50. {
  51. OnMouseConnected();
  52. }
  53. }
  54. }
  55. @Override
  56. public void onInputDeviceChanged(int deviceId)
  57. {
  58. // do nothing
  59. }
  60. @Override
  61. public void onInputDeviceRemoved(int deviceId)
  62. {
  63. if (m_mouseDeviceIds.contains(deviceId))
  64. {
  65. m_mouseDeviceIds.remove(deviceId);
  66. // only inform the native code if we change to having no mice connected
  67. if (m_mouseDeviceIds.size() == 0)
  68. {
  69. OnMouseDisconnected();
  70. }
  71. }
  72. }
  73. public boolean IsConnected()
  74. {
  75. return (m_mouseDeviceIds.size() > 0);
  76. }
  77. private boolean IsMouseDevice(int deviceId)
  78. {
  79. InputDevice device = m_inputManager.getInputDevice(deviceId);
  80. if (device == null)
  81. {
  82. return false;
  83. }
  84. int sources = device.getSources();
  85. return (sources == InputDevice.SOURCE_MOUSE);
  86. }
  87. private InputManager m_inputManager = null;
  88. private Set<Integer> m_mouseDeviceIds = new HashSet<>();
  89. }