Add JobService to delete obsolete anomaly data

Bug: 72385333
Test: RunSettingsRoboTests
Change-Id: I73e16b9785fe0e832acc9e4256c8f9fd1333721e
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index c039041..9332ac8 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3306,6 +3306,9 @@
             </intent-filter>
         </receiver>
 
+        <service android:name=".fuelgauge.batterytip.AnomalyCleanUpJobService"
+                 android:permission="android.permission.BIND_JOB_SERVICE" />
+
         <!-- This is the longest AndroidManifest.xml ever. -->
     </application>
 </manifest>
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 66af163..d5c9291 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -18,6 +18,7 @@
 -->
 <resources>
     <item type="id" name="preference_highlighted" />
+    <item type="id" name="job_anomaly_clean_up" />
 
     <item type="id" name="lock_none" />
     <item type="id" name="lock_pin" />
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobService.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobService.java
new file mode 100644
index 0000000..d5f4879
--- /dev/null
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobService.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.fuelgauge.batterytip;
+
+import android.app.job.JobInfo;
+import android.app.job.JobParameters;
+import android.app.job.JobScheduler;
+import android.app.job.JobService;
+import android.content.ComponentName;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.support.annotation.VisibleForTesting;
+import android.util.Log;
+
+import com.android.settings.R;
+import com.android.settingslib.utils.ThreadUtils;
+
+import java.util.concurrent.TimeUnit;
+
+/** A JobService to clean up obsolete data in anomaly database */
+public class AnomalyCleanUpJobService extends JobService {
+    private static final String TAG = "AnomalyCleanUpJobService";
+
+    @VisibleForTesting
+    static final long CLEAN_UP_FREQUENCY_MS = TimeUnit.DAYS.toMillis(1);
+
+    public static void scheduleCleanUp(Context context) {
+        final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
+
+        final ComponentName component = new ComponentName(context, AnomalyCleanUpJobService.class);
+        final JobInfo.Builder jobBuilder =
+                new JobInfo.Builder(R.id.job_anomaly_clean_up, component)
+                .setMinimumLatency(CLEAN_UP_FREQUENCY_MS)
+                .setRequiresDeviceIdle(true)
+                .setPersisted(true);
+
+        if (jobScheduler.schedule(jobBuilder.build()) != JobScheduler.RESULT_SUCCESS) {
+            Log.i(TAG, "Anomaly clean up job service schedule failed.");
+        }
+    }
+
+    @Override
+    public boolean onStartJob(JobParameters params) {
+        final BatteryDatabaseManager batteryDatabaseManager = BatteryDatabaseManager
+                .getInstance(this);
+        final BatteryTipPolicy policy = new BatteryTipPolicy(this);
+        ThreadUtils.postOnBackgroundThread(() -> {
+            batteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(
+                    System.currentTimeMillis() - TimeUnit.HOURS.toMillis(
+                            policy.dataHistoryRetainHour));
+            jobFinished(params, false /* wantsReschedule */);
+        });
+
+        return true;
+    }
+
+    @Override
+    public boolean onStopJob(JobParameters jobParameters) {
+        return true;
+    }
+}
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
index 834a355..88f399f 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
@@ -36,7 +36,7 @@
 
     @Override
     public void onReceive(Context context, Intent intent) {
-        final BatteryDatabaseManager databaseManager = new BatteryDatabaseManager(context);
+        final BatteryDatabaseManager databaseManager = BatteryDatabaseManager.getInstance(context);
         final BatteryUtils batteryUtils = BatteryUtils.getInstance(context);
         final long configUid = intent.getLongExtra(StatsManager.EXTRA_STATS_CONFIG_UID, -1);
         final long configKey = intent.getLongExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, -1);
@@ -46,6 +46,8 @@
         Log.i(TAG, "Anomaly intent received.  configUid = " + configUid + " configKey = "
                 + configKey + " subscriptionId = " + subscriptionId);
         saveAnomalyToDatabase(databaseManager, batteryUtils, intent);
+
+        AnomalyCleanUpJobService.scheduleCleanUp(context);
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicy.java b/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicy.java
index a580db1..4a7b51d 100644
--- a/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicy.java
+++ b/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicy.java
@@ -19,7 +19,6 @@
 import android.content.Context;
 import android.provider.Settings;
 import android.support.annotation.VisibleForTesting;
-import android.text.format.DateUtils;
 import android.util.KeyValueListParser;
 import android.util.Log;
 
@@ -44,6 +43,7 @@
     private static final String KEY_REDUCED_BATTERY_PERCENT = "reduced_battery_percent";
     private static final String KEY_LOW_BATTERY_ENABLED = "low_battery_enabled";
     private static final String KEY_LOW_BATTERY_HOUR = "low_battery_hour";
+    private static final String KEY_DATA_HISTORY_RETAIN_HOUR = "data_history_retain_hour";
 
     /**
      * {@code true} if general battery tip is enabled
@@ -143,6 +143,14 @@
      */
     public final int lowBatteryHour;
 
+    /**
+     * TTL hour for anomaly data stored in database
+     *
+     * @see Settings.Global#BATTERY_TIP_CONSTANTS
+     * @see #KEY_DATA_HISTORY_RETAIN_HOUR
+     */
+    public final int dataHistoryRetainHour;
+
     private final KeyValueListParser mParser;
 
     public BatteryTipPolicy(Context context) {
@@ -174,6 +182,7 @@
         reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50);
         lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, false);
         lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 16);
+        dataHistoryRetainHour = mParser.getInt(KEY_DATA_HISTORY_RETAIN_HOUR, 72);
     }
 
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobServiceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobServiceTest.java
new file mode 100644
index 0000000..3da7bbd
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanUpJobServiceTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.fuelgauge.batterytip;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertEquals;
+import static org.robolectric.RuntimeEnvironment.application;
+
+import android.app.job.JobInfo;
+import android.app.job.JobScheduler;
+
+import com.android.settings.R;
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowJobScheduler;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class AnomalyCleanUpJobServiceTest {
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void testScheduleCleanUp() {
+        AnomalyCleanUpJobService.scheduleCleanUp(application);
+
+        ShadowJobScheduler shadowJobScheduler = Shadows.shadowOf(
+                application.getSystemService(JobScheduler.class));
+        List<JobInfo> pendingJobs = shadowJobScheduler.getAllPendingJobs();
+        assertEquals(1, pendingJobs.size());
+        JobInfo pendingJob = pendingJobs.get(0);
+        assertThat(pendingJob.getId()).isEqualTo(R.id.job_anomaly_clean_up);
+        assertThat(pendingJob.getMinLatencyMillis()).isEqualTo(TimeUnit.DAYS.toMillis(1));
+        assertThat(pendingJob.isRequireDeviceIdle()).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicyTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicyTest.java
index 78c86f8..f36acee 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicyTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipPolicyTest.java
@@ -49,7 +49,8 @@
             + ",reduced_battery_enabled=true"
             + ",reduced_battery_percent=30"
             + ",low_battery_enabled=false"
-            + ",low_battery_hour=10";
+            + ",low_battery_hour=10"
+            + ",data_history_retain_hour=24";
     private Context mContext;
 
     @Before
@@ -76,6 +77,7 @@
         assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(30);
         assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
         assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
+        assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(24);
     }
 
     @Test
@@ -97,6 +99,7 @@
         assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
         assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
         assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
+        assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(72);
     }
 
 }