Some refactoring in BackgroundDexOptService.

am: 95176bb18e

Change-Id: I191aeed8a7264bb13c4cef8b1ba135fd03a75d58
This commit is contained in:
Calin Juravle
2017-01-11 02:27:25 +00:00
committed by android-build-merger

View File

@@ -69,7 +69,7 @@ public class BackgroundDexOptService extends JobService {
*/ */
final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false); final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false);
private final File dataDir = Environment.getDataDirectory(); private final File mDataDir = Environment.getDataDirectory();
public static void schedule(Context context) { public static void schedule(Context context) {
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
@@ -120,7 +120,7 @@ public class BackgroundDexOptService extends JobService {
private long getLowStorageThreshold() { private long getLowStorageThreshold() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
final long lowThreshold = StorageManager.from(this).getStorageLowBytes(dataDir); final long lowThreshold = StorageManager.from(this).getStorageLowBytes(mDataDir);
if (lowThreshold == 0) { if (lowThreshold == 0) {
Log.e(TAG, "Invalid low storage threshold"); Log.e(TAG, "Invalid low storage threshold");
} }
@@ -134,17 +134,25 @@ public class BackgroundDexOptService extends JobService {
// This job has already been superseded. Do not start it. // This job has already been superseded. Do not start it.
return false; return false;
} }
// Load low battery threshold from the system config. This is a 0-100 integer.
final int lowBatteryThreshold = getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
final long lowThreshold = getLowStorageThreshold();
mAbortPostBootUpdate.set(false);
new Thread("BackgroundDexOptService_PostBootUpdate") { new Thread("BackgroundDexOptService_PostBootUpdate") {
@Override @Override
public void run() { public void run() {
postBootUpdate(jobParams, pm, pkgs);
}
}.start();
return true;
}
private void postBootUpdate(JobParameters jobParams, PackageManagerService pm,
ArraySet<String> pkgs) {
// Load low battery threshold from the system config. This is a 0-100 integer.
final int lowBatteryThreshold = getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
final long lowThreshold = getLowStorageThreshold();
mAbortPostBootUpdate.set(false);
for (String pkg : pkgs) { for (String pkg : pkgs) {
if (mAbortPostBootUpdate.get()) { if (mAbortPostBootUpdate.get()) {
// JobScheduler requested an early abort. // JobScheduler requested an early abort.
@@ -158,7 +166,7 @@ public class BackgroundDexOptService extends JobService {
// Rather bail than completely drain the battery. // Rather bail than completely drain the battery.
break; break;
} }
long usableSpace = dataDir.getUsableSpace(); long usableSpace = mDataDir.getUsableSpace();
if (usableSpace < lowThreshold) { if (usableSpace < lowThreshold) {
// Rather bail than completely fill up the disk. // Rather bail than completely fill up the disk.
Log.w(TAG, "Aborting background dex opt job due to low storage: " + Log.w(TAG, "Aborting background dex opt job due to low storage: " +
@@ -186,33 +194,40 @@ public class BackgroundDexOptService extends JobService {
// Ran to completion, so we abandon our timeslice and do not reschedule. // Ran to completion, so we abandon our timeslice and do not reschedule.
jobFinished(jobParams, /* reschedule */ false); jobFinished(jobParams, /* reschedule */ false);
} }
private boolean runIdleOptimization(final JobParameters jobParams,
final PackageManagerService pm, final ArraySet<String> pkgs) {
new Thread("BackgroundDexOptService_IdleOptimization") {
@Override
public void run() {
idleOptimization(jobParams, pm, pkgs);
}
}.start(); }.start();
return true; return true;
} }
private boolean runIdleOptimization(final JobParameters jobParams, private void idleOptimization(JobParameters jobParams, PackageManagerService pm,
final PackageManagerService pm, final ArraySet<String> pkgs) { ArraySet<String> pkgs) {
// If post-boot update is still running, request that it exits early. // If post-boot update is still running, request that it exits early.
mExitPostBootUpdate.set(true); mExitPostBootUpdate.set(true);
mAbortIdleOptimization.set(false); mAbortIdleOptimization.set(false);
final long lowThreshold = getLowStorageThreshold(); final long lowThreshold = getLowStorageThreshold();
new Thread("BackgroundDexOptService_IdleOptimization") {
@Override
public void run() {
for (String pkg : pkgs) { for (String pkg : pkgs) {
if (mAbortIdleOptimization.get()) { if (mAbortIdleOptimization.get()) {
// JobScheduler requested an early abort. // JobScheduler requested an early abort.
return; return;
} }
synchronized (sFailedPackageNames) {
if (sFailedPackageNames.contains(pkg)) { if (sFailedPackageNames.contains(pkg)) {
// Skip previously failing package // Skip previously failing package
continue; continue;
} }
}
long usableSpace = dataDir.getUsableSpace(); long usableSpace = mDataDir.getUsableSpace();
if (usableSpace < lowThreshold) { if (usableSpace < lowThreshold) {
// Rather bail than completely fill up the disk. // Rather bail than completely fill up the disk.
Log.w(TAG, "Aborting background dex opt job due to low storage: " + Log.w(TAG, "Aborting background dex opt job due to low storage: " +
@@ -240,9 +255,6 @@ public class BackgroundDexOptService extends JobService {
// Ran to completion, so we abandon our timeslice and do not reschedule. // Ran to completion, so we abandon our timeslice and do not reschedule.
jobFinished(jobParams, /* reschedule */ false); jobFinished(jobParams, /* reschedule */ false);
} }
}.start();
return true;
}
@Override @Override
public boolean onStartJob(JobParameters params) { public boolean onStartJob(JobParameters params) {