Merge "Change the logic to check car idleness" into sc-dev am: e1f4e0f0eb am: c3d2f9fa29

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15020076

Change-Id: I3f9bf3b1708e7e6e01cdd56402ea06d486ee045e
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
index 1e5b84d..9ada8dc 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
@@ -30,6 +30,12 @@
 
 import java.io.PrintWriter;
 
+/**
+ * CarIdlenessTracker determines that a car is in idle state when 1) garage mode is started, or
+ * 2) screen is off and idle maintenance is triggered.
+ * If idleness is forced or garage mode is running, the car is considered idle regardless of screen
+ * on/off.
+ */
 public final class CarIdlenessTracker extends BroadcastReceiver implements IdlenessTracker {
     private static final String TAG = "JobScheduler.CarIdlenessTracker";
     private static final boolean DEBUG = JobSchedulerService.DEBUG
@@ -48,6 +54,7 @@
     private boolean mIdle;
     private boolean mGarageModeOn;
     private boolean mForced;
+    private boolean mScreenOn;
     private IdlenessListener mIdleListener;
 
     public CarIdlenessTracker() {
@@ -56,6 +63,7 @@
         mIdle = false;
         mGarageModeOn = false;
         mForced = false;
+        mScreenOn = true;
     }
 
     @Override
@@ -71,6 +79,7 @@
 
         // Screen state
         filter.addAction(Intent.ACTION_SCREEN_ON);
+        filter.addAction(Intent.ACTION_SCREEN_OFF);
 
         // State of GarageMode
         filter.addAction(ACTION_GARAGE_MODE_ON);
@@ -88,6 +97,8 @@
     public void dump(PrintWriter pw) {
         pw.print("  mIdle: "); pw.println(mIdle);
         pw.print("  mGarageModeOn: "); pw.println(mGarageModeOn);
+        pw.print("  mForced: "); pw.println(mForced);
+        pw.print("  mScreenOn: "); pw.println(mScreenOn);
     }
 
     @Override
@@ -121,6 +132,9 @@
         } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
             logIfDebug("Screen is on...");
             handleScreenOn();
+        } else if (action.equals(intent.ACTION_SCREEN_OFF)) {
+            logIfDebug("Screen is off...");
+            mScreenOn = false;
         } else if (action.equals(ACTION_GARAGE_MODE_ON)) {
             logIfDebug("GarageMode is on...");
             mGarageModeOn = true;
@@ -132,10 +146,10 @@
         } else if (action.equals(ActivityManagerService.ACTION_TRIGGER_IDLE)) {
             if (!mGarageModeOn) {
                 logIfDebug("Idle trigger fired...");
-                triggerIdlenessOnce();
+                triggerIdleness();
             } else {
-                logIfDebug("TRIGGER_IDLE received but not changing state; idle="
-                        + mIdle + " screen=" + mGarageModeOn);
+                logIfDebug("TRIGGER_IDLE received but not changing state; mIdle="
+                        + mIdle + " mGarageModeOn=" + mGarageModeOn);
             }
         }
     }
@@ -158,20 +172,24 @@
         }
     }
 
-    private void triggerIdlenessOnce() {
+    private void triggerIdleness() {
         // This is simply triggering idleness once until some constraint will switch it back off
         if (mIdle) {
             // Already in idle state. Nothing to do
             logIfDebug("Device is already idle");
-        } else {
+        } else if (!mScreenOn) {
             // Going idle once
-            logIfDebug("Device is going idle once");
+            logIfDebug("Device is going idle");
             mIdle = true;
             mIdleListener.reportNewIdleState(mIdle);
+        } else {
+            logIfDebug("TRIGGER_IDLE received but not changing state: mIdle = " + mIdle
+                    + ", mScreenOn = " + mScreenOn);
         }
     }
 
     private void handleScreenOn() {
+        mScreenOn = true;
         if (mForced || mGarageModeOn) {
             // Even though screen is on, the device remains idle
             logIfDebug("Screen is on, but device cannot exit idle");
@@ -179,6 +197,7 @@
             // Exiting idle
             logIfDebug("Device is exiting idle");
             mIdle = false;
+            mIdleListener.reportNewIdleState(mIdle);
         } else {
             // Already in non-idle state. Nothing to do
             logIfDebug("Device is already non-idle");