0

Physical home button press can be detected easily on most of the devices:

...

public class ExampleAccessibilityService extends AccessibilityService {

    ...

    @Override
    protected boolean onKeyEvent(KeyEvent event) {    
        if (event.getKeyCode() == KEYCODE_HOME && event.getAction() == ACTION_DOWN)
            Log.d("Example", "The home key is pressed.");
        return super.onKeyEvent(event);
    }
}

But the code above doesn't work on some devices that have a pressure-sensitive virtual home button. I suppose these Samsung Galaxy devices are affected: S8, S8+, S9, S9+, Note10, Note10+ and Fold. Officially, it is considered a pressure sensor, not a button.

Screenshot of Samsung Galaxy S9 and S9+ specs on https://www.samsung.com/global/galaxy/galaxy-s9/specs/. Sensors: Iris, Fingerprint, HR, Pressure (!), Gyro, Proximity, Accelerometer, Geomagnetic, RGB Light, Barometer, Hall sensor. Buttons: On/Off, Volume, Bixby. (Home key is not on the list.)

How to read this sensor?

The TYPE_PRESSURE sensor event is related to the barometer, it shows the ambient air pressure in hPa or mbar.

The getPressure() method returns "the size of the capacitive object rather than literal pressure".

I don't need info about the level of pressure, I just want to know if the pressure-sensitive virtual home button is pressed.

Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57

2 Answers2

0

Technically, it is not considered a sensor, so it can't be used, as far as I know.

for (Sensor s : ((SensorManager) getSystemService(SENSOR_SERVICE)).getSensorList(Sensor.TYPE_ALL))
        Log.d("Sensor", s.getName());

I've logged all the available sensors of a device having a pressure-sensitive virtual home button, but the button isn't on the list. I wonder, How to access sensors not listed by SensorManager?

Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57
  • So it can be pressed like a button, makes the device behave like the home button was pressed, but it is considered a sensor instead of a button... But it can't be accessed using the `SensorManager`, while all the other sensors can, so technically it is not considered a sensor... I don't like the way Samsung is letting me know that it's trying to get rid of this button/sensor. As long as it is there, it should be functional. – Tamás Bolvári Apr 07 '20 at 05:51
0

This virtual button is not for application usage and can only be accessed by that Samsung ROM. One can define the pressure required to trigger it. What's the point in reading it anyway? Your device does not have any pressure sensor, aka barometer. What you mean is the touchscreen, which is pressure sensitive - and so the only chance to read it might be the touchscreen driver.

These are the sources for their vendor binaries, for AOSP. Unless building a custom ROM, there probably is no way to bind custom functionality to the touchscreen or to expose the value to the SDK. These sources should also show, what the virtual button actually does, beside emulating a hardware button event.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Samsung doesn't want my app to use the home button, I get it. They want it to be acccessed only by the Samsung ROM, I see. But my app needs that button, so I don't really care about the intentions of Samsung. There must be a way to make it work, even if it's hacky or undocumented. – Tamás Bolvári Apr 07 '20 at 06:32
  • The point of using the home button is accessibility. My `AccessibilityService` helps the user control the device, so that he can simply press the home button to change settings, instead of unlocking the device and digging in the system settings etc. – Tamás Bolvári Apr 07 '20 at 06:39
  • The device does have a pressure sensor and a barometer too. Please check the screenshot in my question of the official Samsung page. The terminology is confusing, indeed. Usually, the pressure sensor is the synonym of a baromater. But in this case, it refers to the pressure-sensitive virtual home button. It is a pressure sensor. It doesn't measure air pressure. It measures if the home button is pressed. – Tamás Bolvári Apr 07 '20 at 06:47
  • It is a capacitive screen, not a resistive one. [_"the device’s home key was installed under the display and utilizes a pressure sensor to respond in the same way a physical button"_](https://news.samsung.com/global/in-depth-look-whats-inside-the-galaxy-s8-and-s8) The screen is touch sensitive, the [`getPressure()`](https://developer.android.com/reference/android/view/MotionEvent.html#getPressure%28%29) method returns ["the size of the capacitive object rather than literal pressure"](https://stackoverflow.com/a/17540281/1293492). – Tamás Bolvári Apr 07 '20 at 06:53
  • What part of the source code should I read, to see what the virtual button actually does, beside emulating a hardware button event? I'm too beginner to find the relevant lines in such a large code archive. (Is it considered emulation, despite the lack of `KEYCODE_HOME ` type `KeyEvent`?) – Tamás Bolvári Apr 07 '20 at 07:03
  • The user should be able to use the home button comfortably, without unlocking the device, even if he is wearing gloves and the screen is wet or turned off. So my app really needs to use the pressure sensor (not the barometer, but the one installed under the display as a home key). – Tamás Bolvári Apr 07 '20 at 07:09