1

This question is part of this #3

Some Chromebooks are laptop and we can also turn it into tablet mode. See images from here

So my question is how to programmatically detect the mode(laptop or tablet) of Chromebook. For this, I did this, but this is only working in Lenovo Flex11, in other Chromebook it is not working

context.getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES;

if this condition returns true that means Chromebook is in tablet mode else in laptop mode

I need to check this because if the Chromebook is in laptop mode than I have to show a prediction bar only in particular Activity. If it is in tablet mode the soft keyboard will appear and the prediction bar is managed by candidate view of InputMethodService

Alexander N.
  • 1,458
  • 14
  • 25
Priyanka
  • 3,369
  • 1
  • 10
  • 33
  • Just checking if you've seen/tried: https://developer.android.com/topic/arc/input-compatibility#input_translation_mode – Morrison Chang Dec 14 '20 at 08:43
  • @MorrisonChang I already added this into the manifest. – Priyanka Dec 14 '20 at 09:46
  • Hmmm...I owned (past tense) such a chromebook that had both modes. It was the "Slate". After 6 or 8 months or so, since I couldn't return it, I gave it away to a friend. One of my dislikes was that I had learned that Google was discontinuing designing any more such models, and would hence forth build only 'clamshell' models. My suspicion is that one of Google's reasons, is that they, too, find the concept of having SOME models capable of both modes, to be problematic. I suspect, since only SOME models have both 'modes', that there is no reliable way to programmatically test for this. – David Dec 15 '20 at 07:48
  • @Dave, is there any other solution for my problem? – Priyanka Dec 15 '20 at 10:56

1 Answers1

1

We use a combination of two different things. The first is we detect if the device is in desktop mode using the following code snippet :

fun isDesktopMode() : Boolean {
   var hasMouse = false
   val hasKeyboard = resources.configuration.keyboard == KEYBOARD_QWERTY
   val isKeyboardUseable = resources.configuration.hardKeyboardHidden == HARDKEYBOARDHIDDEN_NO

   // Check each input device to see if it is a mouse
   val inputManager = getSystemService(Context.INPUT_SERVICE) as InputManager
   for (deviceId in inputManager.inputDeviceIds) {
       val sourceMask = inputManager.getInputDevice(deviceId).sources
       if ((sourceMask or SOURCE_MOUSE == sourceMask) ||
           (sourceMask or SOURCE_TOUCHPAD == sourceMask) ||
           (sourceMask or SOURCE_TRACKBALL == sourceMask)) {
               hasMouse = true
       }
   }

   return hasMouse && hasKeyboard && isKeyboardUseable
}

To detect if a keyboard is plugged in and/or available we use the following listener for listening to whether or not a keyboard has become active:

val inputManager = getSystemService(Context.INPUT_SERVICE) as InputManager
val inputDeviceListener = object: InputManager.InputDeviceListener {
   override fun onInputDeviceRemoved(deviceId: Int) {
       // Be careful checking device here as it is no longer attached to the system
   }

   override fun onInputDeviceAdded(deviceId: Int) {
       // If you want to learn more about what has been attached, check the InputDevice
       // using the id.
       val sourceMask = inputManager.getInputDevice(deviceId).sources
       if (sourceMask or SOURCE_KEYBOARD == sourceMask) {
           //Keyboard has been Added
           append_to_log("A keyboard has been added.")
       }
   }

   override fun onInputDeviceChanged(deviceId: Int) {
       // Best practice is to check for what you care about when things change (ie. are
       // there any keyboards/mice still attached and usable. Users may have multiple
       // devices attached (integrated keyboard a and bluetooth keyboard) and
       // removing one does not mean the other is no longer available.
       isInDesktopMode = isDesktopMode()
   }
}
inputManager.registerInputDeviceListener(inputDeviceListener, null)

This post is licensed under Apache 2.0.

https://developers.google.com/open-source/devplat

Alexander N.
  • 1,458
  • 14
  • 25
  • Thank you for the answer, but I am not getting desktop mode `true` with this code. it is always false in both tablet/laptop mode. And `InputDeviceListener` is not registring in `onResume`. for this, I need to change mode 2 or 3 times and need to change keyboard 2 3 times than this listener will register. – Priyanka Jan 21 '21 at 08:08
  • isDesktopMode is checking for a physical mouse. If that does not meet your needs, I would remove it. – Alexander N. Jan 21 '21 at 20:15
  • I don't need to check for the mouse. But I test it by connecting/disconnecting the physical mouse and also it returns false. I just need to check whether it is in laptop mode or not and I think any keyboard, mouse configurations will not help me because I tried all configs changes. – Priyanka Jan 22 '21 at 04:24