DenyTheFlowerpot / Scrunch

Play sounds whenever you fold or unfold your Galaxy Z Fold.
European Union Public License 1.2
5 stars 2 forks source link

Use DeviceStateManager on Android 12 to detect the fold state #3

Closed zacharee closed 2 years ago

zacharee commented 2 years ago

This PR reworks FoldDetectionStrategy and FoldActionSignalingService to use DeviceStateManager#registerCallback() on Android 12 and up.

Advantages:

Disadvantages:

In changing the FoldDetectionStrategy to work with DeviceStateManager, I had to make some significant changes to how it and the FoldActionSignalingService work. As a result of those changes, it should be easier to potentially add other non-logcat-based strategies if needed.

To access hidden APIs, LSPosed's Hidden API Bypass library is used. This shouldn't have too much overhead, since it's a pure-Java solution. It also relies on the Java Unsafe API, which is unlikely to ever be blocked by Google.

DenyTheFlowerpot commented 2 years ago

@zacharee Hi, thank you very much! Very clean work. I do have a question about it though.

As far as I can tell from the description of the modified android.jar, it is used by replacing one's local copy with it. This will not work with F-Droid distribution because they build each app update from source on their own server before releasing them. Do you know of any way to bundle the JAR file so that their build process will use it?

zacharee commented 2 years ago

I can replace the direct access with reflection.

DenyTheFlowerpot commented 2 years ago

That would be great, I don't think the performance loss will be that obvious.

zacharee commented 2 years ago

The latest commit is using reflection.

DenyTheFlowerpot commented 2 years ago

Looks good! I tested it on my own device and, apart from the issue described in the comment (which I will fix myself), it works as expected. Thank you very much, I am not too familiar with reflection so it didn't cross my mind to use the new DeviceStateManager.