Fix the AnomalyCleanUpJobService
1. Invoke it in BOOT_COMPLETED 2. Set it periodic 3. Change the anomaly history from 3 days to 30 days Bug: 72385333 Test: RunSettingsRoboTests Change-Id: Ib59a85f3dffd6c094ff400a75bec44d4c65bce44
This commit is contained in:
@@ -3296,7 +3296,7 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
<service android:name=".fuelgauge.batterytip.AnomalyCleanUpJobService"
|
<service android:name=".fuelgauge.batterytip.AnomalyCleanupJobService"
|
||||||
android:permission="android.permission.BIND_JOB_SERVICE" />
|
android:permission="android.permission.BIND_JOB_SERVICE" />
|
||||||
|
|
||||||
<service android:name=".fuelgauge.batterytip.AnomalyDetectionJobService"
|
<service android:name=".fuelgauge.batterytip.AnomalyDetectionJobService"
|
||||||
|
@@ -22,7 +22,6 @@ import android.app.job.JobScheduler;
|
|||||||
import android.app.job.JobService;
|
import android.app.job.JobService;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -32,7 +31,7 @@ import com.android.settingslib.utils.ThreadUtils;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/** A JobService to clean up obsolete data in anomaly database */
|
/** 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";
|
private static final String TAG = "AnomalyCleanUpJobService";
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -41,11 +40,12 @@ public class AnomalyCleanUpJobService extends JobService {
|
|||||||
public static void scheduleCleanUp(Context context) {
|
public static void scheduleCleanUp(Context context) {
|
||||||
final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
|
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 =
|
final JobInfo.Builder jobBuilder =
|
||||||
new JobInfo.Builder(R.id.job_anomaly_clean_up, component)
|
new JobInfo.Builder(R.id.job_anomaly_clean_up, component)
|
||||||
.setMinimumLatency(CLEAN_UP_FREQUENCY_MS)
|
.setPeriodic(CLEAN_UP_FREQUENCY_MS)
|
||||||
.setRequiresDeviceIdle(true)
|
.setRequiresDeviceIdle(true)
|
||||||
|
.setRequiresCharging(true)
|
||||||
.setPersisted(true);
|
.setPersisted(true);
|
||||||
|
|
||||||
if (jobScheduler.schedule(jobBuilder.build()) != JobScheduler.RESULT_SUCCESS) {
|
if (jobScheduler.schedule(jobBuilder.build()) != JobScheduler.RESULT_SUCCESS) {
|
||||||
@@ -61,7 +61,7 @@ public class AnomalyCleanUpJobService extends JobService {
|
|||||||
ThreadUtils.postOnBackgroundThread(() -> {
|
ThreadUtils.postOnBackgroundThread(() -> {
|
||||||
batteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(
|
batteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp(
|
||||||
System.currentTimeMillis() - TimeUnit.HOURS.toMillis(
|
System.currentTimeMillis() - TimeUnit.HOURS.toMillis(
|
||||||
policy.dataHistoryRetainHour));
|
policy.dataHistoryRetainDay));
|
||||||
jobFinished(params, false /* wantsReschedule */);
|
jobFinished(params, false /* wantsReschedule */);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -70,6 +70,6 @@ public class AnomalyCleanUpJobService extends JobService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onStopJob(JobParameters jobParameters) {
|
public boolean onStopJob(JobParameters jobParameters) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -54,6 +54,10 @@ public class AnomalyConfigReceiver extends BroadcastReceiver {
|
|||||||
extraIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
extraIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
uploadPendingIntent(statsManager, pendingIntent);
|
uploadPendingIntent(statsManager, pendingIntent);
|
||||||
|
|
||||||
|
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
|
||||||
|
AnomalyCleanupJobService.scheduleCleanUp(context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -44,6 +44,5 @@ public class AnomalyDetectionReceiver extends BroadcastReceiver {
|
|||||||
bundle.putLong(KEY_ANOMALY_TIMESTAMP, System.currentTimeMillis());
|
bundle.putLong(KEY_ANOMALY_TIMESTAMP, System.currentTimeMillis());
|
||||||
|
|
||||||
AnomalyDetectionJobService.scheduleAnomalyDetection(context, intent);
|
AnomalyDetectionJobService.scheduleAnomalyDetection(context, intent);
|
||||||
AnomalyCleanUpJobService.scheduleCleanUp(context);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@ import android.util.KeyValueListParser;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to store the policy for battery tips, which comes from
|
* Class to store the policy for battery tips, which comes from
|
||||||
@@ -43,7 +44,7 @@ public class BatteryTipPolicy {
|
|||||||
private static final String KEY_REDUCED_BATTERY_PERCENT = "reduced_battery_percent";
|
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_ENABLED = "low_battery_enabled";
|
||||||
private static final String KEY_LOW_BATTERY_HOUR = "low_battery_hour";
|
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";
|
private static final String KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE = "excessive_bg_drain_percentage";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -145,18 +146,19 @@ public class BatteryTipPolicy {
|
|||||||
public final int lowBatteryHour;
|
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 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%)
|
* Battery drain percentage threshold for excessive background anomaly(i.e. 10%)
|
||||||
*
|
*
|
||||||
* This is an additional check for excessive background, to check whether battery drain
|
* This is an additional check for excessive background, to check whether battery drain
|
||||||
* for an app is larger than x%
|
* for an app is larger than x%
|
||||||
|
*
|
||||||
* @see Settings.Global#BATTERY_TIP_CONSTANTS
|
* @see Settings.Global#BATTERY_TIP_CONSTANTS
|
||||||
* @see #KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE
|
* @see #KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE
|
||||||
*/
|
*/
|
||||||
@@ -193,7 +195,7 @@ public class BatteryTipPolicy {
|
|||||||
reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50);
|
reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50);
|
||||||
lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, false);
|
lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, false);
|
||||||
lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 16);
|
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);
|
excessiveBgDrainPercentage = mParser.getInt(KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,7 +41,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class AnomalyCleanUpJobServiceTest {
|
public class AnomalyCleanupJobServiceTest {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
@@ -49,7 +49,7 @@ public class AnomalyCleanUpJobServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScheduleCleanUp() {
|
public void testScheduleCleanUp() {
|
||||||
AnomalyCleanUpJobService.scheduleCleanUp(application);
|
AnomalyCleanupJobService.scheduleCleanUp(application);
|
||||||
|
|
||||||
ShadowJobScheduler shadowJobScheduler = Shadows.shadowOf(
|
ShadowJobScheduler shadowJobScheduler = Shadows.shadowOf(
|
||||||
application.getSystemService(JobScheduler.class));
|
application.getSystemService(JobScheduler.class));
|
||||||
@@ -57,7 +57,8 @@ public class AnomalyCleanUpJobServiceTest {
|
|||||||
assertEquals(1, pendingJobs.size());
|
assertEquals(1, pendingJobs.size());
|
||||||
JobInfo pendingJob = pendingJobs.get(0);
|
JobInfo pendingJob = pendingJobs.get(0);
|
||||||
assertThat(pendingJob.getId()).isEqualTo(R.id.job_anomaly_clean_up);
|
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.isRequireDeviceIdle()).isTrue();
|
||||||
|
assertThat(pendingJob.isRequireCharging()).isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -50,7 +50,7 @@ public class BatteryTipPolicyTest {
|
|||||||
+ ",reduced_battery_percent=30"
|
+ ",reduced_battery_percent=30"
|
||||||
+ ",low_battery_enabled=false"
|
+ ",low_battery_enabled=false"
|
||||||
+ ",low_battery_hour=10"
|
+ ",low_battery_hour=10"
|
||||||
+ ",data_history_retain_hour=24"
|
+ ",data_history_retain_day=24"
|
||||||
+ ",excessive_bg_drain_percentage=25";
|
+ ",excessive_bg_drain_percentage=25";
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ public class BatteryTipPolicyTest {
|
|||||||
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(30);
|
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(30);
|
||||||
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
|
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
|
||||||
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
|
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
|
||||||
assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(24);
|
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(24);
|
||||||
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(25);
|
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(25);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ public class BatteryTipPolicyTest {
|
|||||||
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
|
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
|
||||||
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
|
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
|
||||||
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
|
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
|
||||||
assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(72);
|
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30);
|
||||||
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
|
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user