Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Brightness: darker than 80 % for real

מקס חייםa3b1ddefceff
diff --git a/addons/brightness/README.md b/addons/brightness/README.md
index 8ded8968a..dca04dbac 100644
--- a/addons/brightness/README.md
+++ b/addons/brightness/README.md
@@ -47,8 +47,8 @@ On other TVs these rows don't appear.
## How it works
This is software dimming. A full-screen window of one colour sits over everything (`TYPE_APPLICATION_OVERLAY`),
-black at the missing brightness with the amber folded in for warmth. It never takes focus, keys or touches, so
-the remote keeps controlling whatever is on screen. A foreground service holds the window and switches night
+black at the missing brightness with the amber folded in for warmth. It never takes focus or keys, so the remote
+keeps controlling whatever is on screen. A foreground service holds the window and switches night
mode at the scheduled times, and it stops itself when there is nothing to draw and no schedule to follow.
## Permissions
@@ -102,6 +102,9 @@ with your own key if you hand them out; the launcher pins its Show answer to the
other apps; `install.sh` allows it over ADB, or the app shows the ADB command.
- **Aggressive memory managers** can stop the service on 1 GB TVs. If they do, the layer goes and the screen
is brighter, never darker; opening the app starts it again.
+- **Below 20 % an air-mouse pointer can't click through.** Since Android 12 a window that lets touches through
+ is drawn at most 80 % opaque, so darker than that the layer keeps touches. TVs have no touchscreen and the
+ remote's keys are never affected; only a pointer (an air mouse) is.
- Screenshots and screen recordings include the layer.
## Tested on
diff --git a/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/DimService.java b/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/DimService.java
index e38b8f9e8..844f6e68e 100644
--- a/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/DimService.java
+++ b/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/DimService.java
@@ -25,8 +25,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
- * Holds the dimming layer: one full-screen window over every app that never takes focus, keys or touches, so the
- * remote keeps controlling what is on screen. Runs only while there is a layer to hold or a schedule to follow,
+ * Holds the dimming layer: one full-screen window over every app that never takes focus or keys, so the remote
+ * keeps controlling what is on screen. Runs only while there is a layer to hold or a schedule to follow,
* and stops itself otherwise. It redraws whenever the settings change and at each start and end of the night.
* With the TV picture option on, it also sets the TV's own picture for the night and puts the day's back after.
*/
@@ -44,6 +44,8 @@ public class DimService extends Service {
private Dimming.Picture tvSent;
private WindowManager wm;
private View layer;
+ /** Whether the layer window keeps touches, which lets Android draw it darker than 80 %. */
+ private boolean layerKeepsTouches;
/** Kept in a field: SharedPreferences holds its listeners weakly. */
private final SharedPreferences.OnSharedPreferenceChangeListener prefsListener = (p, key) -> apply();
private final Runnable tick = this::apply;
@@ -172,15 +174,21 @@ public class DimService extends Service {
}
private void showLayer(int color) {
+ boolean opaque = Dimming.keepsTouches(color);
if (layer != null) {
layer.setBackgroundColor(color);
+ if (opaque != layerKeepsTouches) {
+ wm.updateViewLayout(layer, params(opaque));
+ layerKeepsTouches = opaque;
+ }
return;
}
View v = new View(this);
v.setBackgroundColor(color);
try {
- wm.addView(v, params());
+ wm.addView(v, params(opaque));
layer = v;
+ layerKeepsTouches = opaque;
} catch (RuntimeException e) {
Log.w(Prefs.TAG, "layer not shown (display over other apps not allowed?): " + e.getMessage());
}
@@ -196,13 +204,19 @@ public class DimService extends Service {
layer = null;
}
- private static WindowManager.LayoutParams params() {
+ /**
+ * Since Android 12 a window that lets touches through is drawn at most 80 % opaque, whatever its colour says.
+ * Darker than that, the layer keeps touches instead: TVs have no touchscreen, and the remote's keys never come
+ * here (not focusable); only an air-mouse pointer can't click through then.
+ */
+ private static WindowManager.LayoutParams params(boolean keepTouches) {
+ int touch = keepTouches ? WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL : WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
WindowManager.LayoutParams p = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
+ | touch
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
diff --git a/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/Dimming.java b/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/Dimming.java
index 160093f27..e51092b3a 100644
--- a/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/Dimming.java
+++ b/addons/brightness/app/src/main/java/io/github/maxhayim/brightness/Dimming.java
@@ -176,6 +176,14 @@ final class Dimming {
return a << 24 | r << 16 | g << 8 | b;
}
+ /** Android draws a window that lets touches through at most this opaque (InputManager's default). */
+ static final int MAX_PASS_THROUGH_ALPHA = Math.round(0.8f * 255);
+
+ /** True when the layer must keep touches to be drawn as dark as its colour says. */
+ static boolean keepsTouches(int color) {
+ return (color >>> 24) > MAX_PASS_THROUGH_ALPHA;
+ }
+
/**
* Minutes from minute until the level can next change by itself: the next start or end of the night window.
* -1 when nothing changes on its own (dimming off, or night mode not scheduled, or an empty window).
diff --git a/addons/brightness/app/src/test/java/io/github/maxhayim/brightness/DimmingTest.java b/addons/brightness/app/src/test/java/io/github/maxhayim/brightness/DimmingTest.java
index 8555aeadf..d379e7eda 100644
--- a/addons/brightness/app/src/test/java/io/github/maxhayim/brightness/DimmingTest.java
+++ b/addons/brightness/app/src/test/java/io/github/maxhayim/brightness/DimmingTest.java
@@ -153,6 +153,14 @@ public class DimmingTest {
assertFalse(p.equals(p.withDynamicBacklight(false)));
}
+ @Test
+ public void onlyLayersDarkerThanAndroidsPassThroughCapKeepTouches() {
+ assertFalse(Dimming.keepsTouches(Dimming.color(new Dimming.Level(70, 0, false))));
+ assertFalse("80 % dark is still drawn as such", Dimming.keepsTouches(Dimming.color(new Dimming.Level(20, 0, false))));
+ assertTrue(Dimming.keepsTouches(Dimming.color(new Dimming.Level(10, 0, false))));
+ assertTrue("warmth adds to the darkness", Dimming.keepsTouches(Dimming.color(new Dimming.Level(25, 100, true))));
+ }
+
@Test
public void windowPastMidnight() {
int start = 21 * 60, end = 7 * 60;