</intent-filter>
</receiver>
- <service android:name=".fuelgauge.batterytip.AnomalyCleanUpJobService"
+ <service android:name=".fuelgauge.batterytip.AnomalyCleanupJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service android:name=".fuelgauge.batterytip.AnomalyDetectionJobService"
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 java.util.concurrent.TimeUnit;
/** A JobService to clean up obsolete data in anomaly database */
-public class AnomalyCleanUpJobService extends JobService {
+public class AnomalyCleanupJobService extends JobService {
private static final String TAG = "AnomalyCleanUpJobService";
@VisibleForTesting
public static void scheduleCleanUp(Context context) {
final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
- final ComponentName component = new ComponentName(context, AnomalyCleanUpJobService.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);
+ .setPeriodic(CLEAN_UP_FREQUENCY_MS)
+ .setRequiresDeviceIdle(true)
+ .setRequiresCharging(true)
+ .setPersisted(true);
if (jobScheduler.schedule(jobBuilder.build()) != JobScheduler.RESULT_SUCCESS) {
Log.i(TAG, "Anomaly clean up job service schedule failed.");
ThreadUtils.postOnBackgroundThread(() -> {
batteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(
System.currentTimeMillis() - TimeUnit.HOURS.toMillis(
- policy.dataHistoryRetainHour));
+ policy.dataHistoryRetainDay));
jobFinished(params, false /* wantsReschedule */);
});
@Override
public boolean onStopJob(JobParameters jobParameters) {
- return true;
+ return false;
}
}
extraIntent, PendingIntent.FLAG_UPDATE_CURRENT);
uploadPendingIntent(statsManager, pendingIntent);
+
+ if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
+ AnomalyCleanupJobService.scheduleCleanUp(context);
+ }
}
}
bundle.putLong(KEY_ANOMALY_TIMESTAMP, System.currentTimeMillis());
AnomalyDetectionJobService.scheduleAnomalyDetection(context, intent);
- AnomalyCleanUpJobService.scheduleCleanUp(context);
}
}
import android.util.Log;
import java.time.Duration;
+import java.util.concurrent.TimeUnit;
/**
* Class to store the policy for battery tips, which comes from
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";
+ private static final String KEY_DATA_HISTORY_RETAIN_DAY = "data_history_retain_day";
private static final String KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE = "excessive_bg_drain_percentage";
/**
public final int lowBatteryHour;
/**
- * TTL hour for anomaly data stored in database
+ * TTL day for anomaly data stored in database
*
* @see Settings.Global#BATTERY_TIP_CONSTANTS
- * @see #KEY_DATA_HISTORY_RETAIN_HOUR
+ * @see #KEY_DATA_HISTORY_RETAIN_DAY
*/
- public final int dataHistoryRetainHour;
+ public final int dataHistoryRetainDay;
/**
* Battery drain percentage threshold for excessive background anomaly(i.e. 10%)
*
* This is an additional check for excessive background, to check whether battery drain
* for an app is larger than x%
+ *
* @see Settings.Global#BATTERY_TIP_CONSTANTS
* @see #KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE
*/
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);
+ dataHistoryRetainDay = mParser.getInt(KEY_DATA_HISTORY_RETAIN_DAY, 30);
excessiveBgDrainPercentage = mParser.getInt(KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE, 10);
}
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
-public class AnomalyCleanUpJobServiceTest {
+public class AnomalyCleanupJobServiceTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@Test
public void testScheduleCleanUp() {
- AnomalyCleanUpJobService.scheduleCleanUp(application);
+ AnomalyCleanupJobService.scheduleCleanUp(application);
ShadowJobScheduler shadowJobScheduler = Shadows.shadowOf(
application.getSystemService(JobScheduler.class));
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.getIntervalMillis()).isEqualTo(TimeUnit.DAYS.toMillis(1));
assertThat(pendingJob.isRequireDeviceIdle()).isTrue();
+ assertThat(pendingJob.isRequireCharging()).isTrue();
}
}
+ ",reduced_battery_percent=30"
+ ",low_battery_enabled=false"
+ ",low_battery_hour=10"
- + ",data_history_retain_hour=24"
+ + ",data_history_retain_day=24"
+ ",excessive_bg_drain_percentage=25";
private Context mContext;
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(30);
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
- assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(24);
+ assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(24);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(25);
}
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
- assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(72);
+ assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
}
}