[DO NOT MERGE] Handle config override via settings correctly

Initialize persistence only after we can read the settings
provider as we need to take into account the current config
potentially set as an override in settings vs using the
hard coded defaults.

Make the last persist time more robust to ensure it always
termines to guard against other unforeseen cases wehere the
persistet files don't match what is expected under the current
config.

Test: atest CtsAppOpsTestCases
Test: atest android.appsecurity.cts.AppOpsTest

Bug: 134093967

Change-Id: I4bd272d3908eb8fc2afcdb7c1bd96b05a182d8c4
diff --git a/services/tests/servicestests/src/com/android/server/appop/AppOpsServiceTest.java b/services/tests/servicestests/src/com/android/server/appop/AppOpsServiceTest.java
index d901179..552058f 100644
--- a/services/tests/servicestests/src/com/android/server/appop/AppOpsServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/appop/AppOpsServiceTest.java
@@ -37,12 +37,15 @@
 import android.os.HandlerThread;
 import android.os.Process;
 import android.os.RemoteCallback;
+import android.provider.Settings;
 
 import androidx.test.InstrumentationRegistry;
 import androidx.test.filters.SmallTest;
 import androidx.test.runner.AndroidJUnit4;
 
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -68,11 +71,14 @@
     private File mAppOpsFile;
     private Context mContext;
     private Handler mHandler;
+    private AppOpsManager mAppOpsManager;
     private AppOpsService mAppOpsService;
     private String mMyPackageName;
     private int mMyUid;
     private long mTestStartMillis;
 
+    private static String sDefaultAppopHistoryParameters;
+
     @Before
     public void setUp() {
         mContext = InstrumentationRegistry.getTargetContext();
@@ -88,11 +94,29 @@
         mMyPackageName = mContext.getOpPackageName();
         mMyUid = Process.myUid();
 
+        mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
         mAppOpsService = new AppOpsService(mAppOpsFile, mHandler);
+        mAppOpsService.mHistoricalRegistry.systemReady(mContext.getContentResolver());
         mAppOpsService.mContext = mContext;
         mTestStartMillis = System.currentTimeMillis();
     }
 
+    @BeforeClass
+    public static void configureDesiredAppopHistoryParameters() {
+        final Context context = InstrumentationRegistry.getTargetContext();
+        sDefaultAppopHistoryParameters = Settings.Global.getString(context.getContentResolver(),
+                Settings.Global.APPOP_HISTORY_PARAMETERS);
+        Settings.Global.putString(InstrumentationRegistry.getTargetContext().getContentResolver(),
+                Settings.Global.APPOP_HISTORY_PARAMETERS, null);
+    }
+
+    @AfterClass
+    public static void restoreDefaultAppopHistoryParameters() {
+        Settings.Global.putString(InstrumentationRegistry.getTargetContext().getContentResolver(),
+                Settings.Global.APPOP_HISTORY_PARAMETERS,
+                sDefaultAppopHistoryParameters);
+    }
+
     @Test
     public void testGetOpsForPackage_noOpsLogged() {
         assertThat(getLoggedOps()).isNull();