Correctly offset app op history on reboot

The offset was computed based off the offset represented by
the most recent historical file while we have to use the time
this file was written.

Also now we persist the history on reboot and shutdown,
significantly minimizing the possibility of data loss.

Added test API to emulate reboot of the history to allow for
precise and tightly controlled test to prevent flakes due to
boot time deviations.

Test: atest android.app.appops.cts.HistoricalAppopsTest#testRebootHistory

Fixes:156853195

Change-Id: I4142371f8bc2b1d710cc8c300e7e79cb03764c04
diff --git a/core/java/android/app/AppOpsManager.java b/core/java/android/app/AppOpsManager.java
index b058dcd..50bb9a3 100644
--- a/core/java/android/app/AppOpsManager.java
+++ b/core/java/android/app/AppOpsManager.java
@@ -8577,6 +8577,25 @@
     }
 
     /**
+     * Reboots the ops history.
+     *
+     * @param offlineDurationMillis The duration to wait between
+     * tearing down and initializing the history. Must be greater
+     * than or equal to zero.
+     *
+     * @hide
+     */
+    @TestApi
+    @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
+    public void rebootHistory(long offlineDurationMillis) {
+        try {
+            mService.rebootHistory(offlineDurationMillis);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Pulls current AppOps access report and picks package and op to watch for next access report
      * Returns null if no reports were collected since last call. There is no guarantee of report
      * collection, hence this method should be called periodically even if no report was collected