Perform idle maintenance when the device is charging.

Added the precondition that the device should be charging to the
existing ones of the device not being used for awhile and the
battery level being high enough. Note that even if the device is
charging, we have to check the battery level since the user can
unplug it at any time.

bug:8688454

Change-Id: I709b1620571301743dc3504a6a625e2018951bfa
diff --git a/services/java/com/android/server/EventLogTags.logtags b/services/java/com/android/server/EventLogTags.logtags
index 59577ad..8eaa91d 100644
--- a/services/java/com/android/server/EventLogTags.logtags
+++ b/services/java/com/android/server/EventLogTags.logtags
@@ -162,3 +162,15 @@
 # IntentFirewall.java
 # ---------------------------
 51400 ifw_intent_matched (Intent Type|1|5),(Component Name|3),(Caller Uid|1|5),(Caller Pkg Count|1|1),(Caller Pkgs|3),(Action|3),(MIME Type|3),(URI|3),(Flags|1|5)
+
+# ---------------------------
+# IdleMaintenanceService.java
+# ---------------------------
+2753 idle_maintenance_window_start (time|2|3), (lastUserActivity|2|3), (batteryLevel|1|6), (batteryCharging|1|5)
+2754 idle_maintenance_window_finish (time|2|3), (lastUserActivity|2|3), (batteryLevel|1|6), (batteryCharging|1|5)
+
+# ---------------------------
+# MountService.java
+# ---------------------------
+2755 fstrim_start (time|2|3)
+2756 fstrim_finish (time|2|3)
diff --git a/services/java/com/android/server/IdleMaintenanceService.java b/services/java/com/android/server/IdleMaintenanceService.java
index a7442f6..0c90de4 100644
--- a/services/java/com/android/server/IdleMaintenanceService.java
+++ b/services/java/com/android/server/IdleMaintenanceService.java
@@ -30,9 +30,6 @@
 import android.os.UserHandle;
 import android.util.Log;
 
-import java.util.Calendar;
-import java.util.TimeZone;
-
 /**
  * This service observes the device state and when applicable sends
  * broadcasts at the beginning and at the end of a period during which
@@ -57,9 +54,15 @@
 
     private static final int LAST_USER_ACTIVITY_TIME_INVALID = -1;
 
-    private static final int MIN_IDLE_MAINTENANCE_START_BATTERY_LEVEL = 20; // percent
+    private static final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
 
-    private static final long MIN_IDLE_MAINTENANCE_START_USER_INACTIVITY = 60 * 60 * 1000; // 1 hour
+    private static final int MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_START_CHARGING = 30; // percent
+
+    private static final int MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_START_NOT_CHARGING = 80; // percent
+
+    private static final int MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_RUNNING = 10; // percent
+
+    private static final long MIN_USER_INACTIVITY_IDLE_MAINTENANCE_START = 60 * 60 * 1000; // 1 hour
 
     private final Intent mIdleMaintenanceStartIntent =
             new Intent(Intent.ACTION_IDLE_MAINTENANCE_START);
@@ -73,14 +76,14 @@
 
     private final Handler mHandler;
 
-    private final Calendar mTempCalendar = Calendar.getInstance();
-
-    private final Calendar mLastIdleMaintenanceStartTime = Calendar.getInstance();
+    private long mLastIdleMaintenanceStartTimeMillis = SystemClock.elapsedRealtime();
 
     private long mLastUserActivityElapsedTimeMillis = LAST_USER_ACTIVITY_TIME_INVALID;
 
     private int mBatteryLevel;
 
+    private boolean mBatteryCharging;
+
     private boolean mIdleMaintenanceStarted;
 
     public IdleMaintenanceService(Context context) {
@@ -91,10 +94,6 @@
 
         mHandler = new Handler(mContext.getMainLooper());
 
-        // Move one day back so we can run maintenance the first day after starting.
-        final int prevDayOfYear = mLastIdleMaintenanceStartTime.get(Calendar.DAY_OF_YEAR) - 1;
-        mLastIdleMaintenanceStartTime.set(Calendar.DAY_OF_YEAR, prevDayOfYear);
-
         register(mContext.getMainLooper());
     }
 
@@ -120,15 +119,21 @@
         if (mIdleMaintenanceStarted) {
             // Idle maintenance can be interrupted only by
             // a change of the device state.
-            if (!deviceStatePermitsIdleMaintenance()) {
+            if (!deviceStatePermitsIdleMaintenanceRunning()) {
                 mIdleMaintenanceStarted = false;
+                EventLogTags.writeIdleMaintenanceWindowFinish(SystemClock.elapsedRealtime(),
+                        mLastUserActivityElapsedTimeMillis, mBatteryLevel,
+                        mBatteryCharging ? 1 : 0);
                 sendIdleMaintenanceEndIntent();
             }
-        } else if (deviceStatePermitsIdleMaintenance()
+        } else if (deviceStatePermitsIdleMaintenanceStart()
                 && lastUserActivityPermitsIdleMaintenanceStart()
                 && lastRunPermitsIdleMaintenanceStart()) {
             mIdleMaintenanceStarted = true;
-            mLastIdleMaintenanceStartTime.setTimeInMillis(System.currentTimeMillis());
+            EventLogTags.writeIdleMaintenanceWindowStart(SystemClock.elapsedRealtime(),
+                    mLastUserActivityElapsedTimeMillis, mBatteryLevel,
+                    mBatteryCharging ? 1 : 0);
+            mLastIdleMaintenanceStartTimeMillis = SystemClock.elapsedRealtime();
             sendIdleMaintenanceStartIntent();
         }
     }
@@ -151,29 +156,26 @@
                 null, this, mHandler, Activity.RESULT_OK, null, null);
     }
 
-    private boolean deviceStatePermitsIdleMaintenance() {
+    private boolean deviceStatePermitsIdleMaintenanceStart() {
+        final int minBatteryLevel = mBatteryCharging
+                ? MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_START_CHARGING
+                : MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_START_NOT_CHARGING;
         return (mLastUserActivityElapsedTimeMillis != LAST_USER_ACTIVITY_TIME_INVALID
-                && mBatteryLevel > MIN_IDLE_MAINTENANCE_START_BATTERY_LEVEL);
+                && mBatteryLevel > minBatteryLevel);
+    }
+
+    private boolean deviceStatePermitsIdleMaintenanceRunning() {
+        return (mLastUserActivityElapsedTimeMillis != LAST_USER_ACTIVITY_TIME_INVALID
+                && mBatteryLevel > MIN_BATTERY_LEVEL_IDLE_MAINTENANCE_RUNNING);
     }
 
     private boolean lastUserActivityPermitsIdleMaintenanceStart() {
         return (SystemClock.elapsedRealtime() - mLastUserActivityElapsedTimeMillis
-                > MIN_IDLE_MAINTENANCE_START_USER_INACTIVITY);
+                > MIN_USER_INACTIVITY_IDLE_MAINTENANCE_START);
     }
 
     private boolean lastRunPermitsIdleMaintenanceStart() {
-        Calendar now = mTempCalendar;
-        // Not setting the Locale since we do not care of locale
-        // specific properties such as the first day of the week.
-        now.setTimeZone(TimeZone.getDefault());
-        now.setTimeInMillis(System.currentTimeMillis());
-
-        Calendar lastRun = mLastIdleMaintenanceStartTime;
-        // Not setting the Locale since we do not care of locale
-        // specific properties such as the first day of the week.
-        lastRun.setTimeZone(TimeZone.getDefault());
-
-        return now.get(Calendar.DAY_OF_YEAR) != lastRun.get(Calendar.DAY_OF_YEAR);
+        return SystemClock.elapsedRealtime() - mLastIdleMaintenanceStartTimeMillis > MILLIS_IN_DAY;
     }
 
     @Override
@@ -186,6 +188,10 @@
             final int maxBatteryLevel = intent.getExtras().getInt(BatteryManager.EXTRA_SCALE);
             final int currBatteryLevel = intent.getExtras().getInt(BatteryManager.EXTRA_LEVEL);
             mBatteryLevel = (int) (((float) maxBatteryLevel / 100) * currBatteryLevel);
+            final int pluggedState = intent.getExtras().getInt(BatteryManager.EXTRA_PLUGGED);
+            final int chargerState = intent.getExtras().getInt(
+                    BatteryManager.EXTRA_INVALID_CHARGER, 0);
+            mBatteryCharging = (pluggedState > 0 && chargerState == 0);
         } else if (Intent.ACTION_SCREEN_ON.equals(action)
                 || Intent.ACTION_DREAMING_STOPPED.equals(action)) {
             mLastUserActivityElapsedTimeMillis = LAST_USER_ACTIVITY_TIME_INVALID;
diff --git a/services/java/com/android/server/MountService.java b/services/java/com/android/server/MountService.java
index e8d7882..82f8d43 100644
--- a/services/java/com/android/server/MountService.java
+++ b/services/java/com/android/server/MountService.java
@@ -36,6 +36,7 @@
 import android.os.Binder;
 import android.os.Environment;
 import android.os.Environment.UserEnvironment;
+import android.os.Build;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IBinder;
@@ -43,6 +44,7 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.storage.IMountService;
@@ -173,6 +175,11 @@
         public static final int VolumeDiskInserted             = 630;
         public static final int VolumeDiskRemoved              = 631;
         public static final int VolumeBadRemoval               = 632;
+
+        /*
+         * 700 series - fstrim
+         */
+        public static final int FstrimCompleted                = 700;
     }
 
     private Context mContext;
@@ -609,6 +616,7 @@
                     // This method runs on the handler thread,
                     // so it is safe to directly call into vold.
                     mConnector.execute("fstrim", "dotrim");
+                    EventLogTags.writeFstrimStart(SystemClock.elapsedRealtime());
                 } catch (NativeDaemonConnectorException ndce) {
                     Slog.e(TAG, "Failed to run fstrim!");
                 }
@@ -857,6 +865,10 @@
                 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
                 updatePublicVolumeState(volume, Environment.MEDIA_BAD_REMOVAL);
                 action = Intent.ACTION_MEDIA_BAD_REMOVAL;
+            } else if (code == VoldResponseCode.FstrimCompleted) {
+                if (Build.IS_DEBUGGABLE) {
+                    EventLogTags.writeFstrimFinish(SystemClock.elapsedRealtime());
+                }
             } else {
                 Slog.e(TAG, String.format("Unknown code {%d}", code));
             }