sdk: livedisplay: add a method to determine whether night mode is enabled

LiveDisplayManager.getMode() returns MODE_AUTO when night mode is enabled
automatically, so there's no way to determine if night is on when livedisplay
changes automatically

Change-Id: Ia47fc127232c2bb3b6634b55712060a919f8c9c1
Signed-off-by: Joey <joey@lineageos.org>
diff --git a/api/lineage_current.txt b/api/lineage_current.txt
index 41aa62f..35620db 100644
--- a/api/lineage_current.txt
+++ b/api/lineage_current.txt
@@ -279,6 +279,7 @@
     method public boolean setMode(int);
     method public boolean setNightColorTemperature(int);
     method public boolean setPictureAdjustment(lineageos.hardware.HSIC);
+    method public boolean isNightModeEnabled();
     field public static final int ADJUSTMENT_CONTRAST = 3; // 0x3
     field public static final int ADJUSTMENT_HUE = 0; // 0x0
     field public static final int ADJUSTMENT_INTENSITY = 2; // 0x2
diff --git a/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayFeature.java b/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayFeature.java
index 37575d2..d327943 100644
--- a/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayFeature.java
+++ b/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayFeature.java
@@ -143,7 +143,7 @@
         return mState.mTwilight;
     }
 
-    protected final boolean isNight() {
+    public final boolean isNight() {
         return mState.mTwilight != null && mState.mTwilight.isNight();
     }
 
diff --git a/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayService.java b/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayService.java
index 52816a7..bc8f2ec 100644
--- a/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayService.java
+++ b/lineage/lib/main/java/org/lineageos/platform/internal/display/LiveDisplayService.java
@@ -370,6 +370,12 @@
                 mFeatures.get(i).dump(pw);
             }
         }
+
+        @Override
+        public boolean isNight() {
+            final TwilightState twilight = mTwilightTracker.getCurrentState();
+            return twilight != null && twilight.isNight();
+        }
     };
 
     // Listener for screen on/off events
diff --git a/sdk/src/java/lineageos/hardware/ILiveDisplayService.aidl b/sdk/src/java/lineageos/hardware/ILiveDisplayService.aidl
index a9a766d..f864ea8 100644
--- a/sdk/src/java/lineageos/hardware/ILiveDisplayService.aidl
+++ b/sdk/src/java/lineageos/hardware/ILiveDisplayService.aidl
@@ -52,4 +52,5 @@
     HSIC getPictureAdjustment();
     HSIC getDefaultPictureAdjustment();
     boolean setPictureAdjustment(in HSIC adj);
+    boolean isNight();
 }
diff --git a/sdk/src/java/lineageos/hardware/LiveDisplayManager.java b/sdk/src/java/lineageos/hardware/LiveDisplayManager.java
index 2ad650c..eb7a575 100644
--- a/sdk/src/java/lineageos/hardware/LiveDisplayManager.java
+++ b/sdk/src/java/lineageos/hardware/LiveDisplayManager.java
@@ -482,4 +482,19 @@
         }
         return null;
     }
+
+    /**
+     * Determine whether night mode is enabled (be it automatic or manual)
+     */
+    public boolean isNightModeEnabled() {
+        // This method might be called before config has been set up
+        // so a NPE would have been thrown, just report night mode is disabled instead
+        try {
+            return getMode() == MODE_NIGHT || sService.isNight();
+        } catch (NullPointerException e) {
+            Log.w(TAG, "Can\'t check whether night mode is enabled because the service isn\'t ready");
+        } catch (RemoteException ignored) {
+        }
+        return false;
+    }
 }