Bug 1407046 - Migrate FileCleanupService to JobIntentService. r=JanH

Also cleaned the code a little.

MozReview-Commit-ID: Bs3bUdxxz8k

--HG--
extra : rebase_source : bf564b683e786010d8f4836a1ee75aefa0514fa8
This commit is contained in:
Petru Lingurar 2018-06-26 17:42:28 +03:00
Родитель d1e02a31c5
Коммит 0ce784d030
5 изменённых файлов: 41 добавлений и 21 удалений

Просмотреть файл

@ -7,6 +7,7 @@
package org.mozilla.gecko.cleanup;
import android.content.Intent;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -18,7 +19,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests the methods of {@link FileCleanupService}.
@ -42,9 +44,8 @@ public class TestFileCleanupService {
private void onHandleIntent(final ArrayList<String> filePaths) {
final FileCleanupService service = new FileCleanupService();
final Intent intent = new Intent(FileCleanupService.ACTION_DELETE_FILES);
intent.putExtra(FileCleanupService.EXTRA_FILE_PATHS_TO_DELETE, filePaths);
service.onHandleIntent(intent);
final Intent fileCleanupIntent = FileCleanupService.getFileCleanupIntent(filePaths);
service.onHandleWork(fileCleanupIntent);
}
@Test

Просмотреть файл

@ -419,7 +419,8 @@
<!-- DON'T EXPORT THIS, please! An attacker could delete arbitrary files. -->
<service
android:exported="false"
android:name="org.mozilla.gecko.cleanup.FileCleanupService">
android:name="org.mozilla.gecko.cleanup.FileCleanupService"
android:permission="android.permission.BIND_JOB_SERVICE" >
</service>
<receiver

Просмотреть файл

@ -34,6 +34,8 @@ public class JobIdsConstants {
private static final int JOB_ID_TELEMETRY_UPLOAD = 1007;
private static final int JOB_ID_FILE_CLEANUP = 1008;
public static int getIdForDlcStudyJob() {
return getIdWithOffset(JOB_ID_DLC_STUDY);
}
@ -66,6 +68,10 @@ public class JobIdsConstants {
return getIdWithOffset(JOB_ID_TELEMETRY_UPLOAD);
}
public static int getIdForFileCleanupJob() {
return getIdWithOffset(JOB_ID_FILE_CLEANUP);
}
private static boolean isReleaseBuild() {
return AppConstants.RELEASE_OR_BETA;
}

Просмотреть файл

@ -53,10 +53,9 @@ public class FileCleanupController {
recordCleanupScheduled(sharedPrefs);
final Intent fileCleanupIntent = new Intent(context, FileCleanupService.class);
fileCleanupIntent.setAction(FileCleanupService.ACTION_DELETE_FILES);
fileCleanupIntent.putExtra(FileCleanupService.EXTRA_FILE_PATHS_TO_DELETE, getFilesToCleanup(profilePath + "/"));
context.startService(fileCleanupIntent);
final Intent fileCleanupIntent =
FileCleanupService.getFileCleanupIntent(getFilesToCleanup(profilePath + "/"));
FileCleanupService.enqueueWork(context, fileCleanupIntent);
}
private static boolean isCleanupReady(final SharedPreferences sharedPrefs) {

Просмотреть файл

@ -6,10 +6,14 @@
package org.mozilla.gecko.cleanup;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import org.mozilla.gecko.JobIdsConstants;
import java.io.File;
import java.util.ArrayList;
@ -30,24 +34,25 @@ import java.util.ArrayList;
*
* The major trade-off is that this Service is very dangerous if it's exported... so don't do that!
*/
public class FileCleanupService extends IntentService {
public class FileCleanupService extends JobIntentService {
private static final String LOGTAG = "Gecko" + FileCleanupService.class.getSimpleName();
private static final String WORKER_THREAD_NAME = LOGTAG + "Worker";
public static final String ACTION_DELETE_FILES = "org.mozilla.gecko.intent.action.DELETE_FILES";
public static final String EXTRA_FILE_PATHS_TO_DELETE = "org.mozilla.gecko.file_paths_to_delete";
private static final String ACTION_DELETE_FILES = "org.mozilla.gecko.intent.action.DELETE_FILES";
private static final String EXTRA_FILE_PATHS_TO_DELETE = "org.mozilla.gecko.file_paths_to_delete";
public FileCleanupService() {
super(WORKER_THREAD_NAME);
public static void enqueueWork(@NonNull final Context context, @NonNull final Intent workIntent) {
enqueueWork(context, FileCleanupService.class, JobIdsConstants.getIdForFileCleanupJob(), workIntent);
}
// We're likely to get scheduled again - let's wait until then in order to avoid:
// * The coding complexity of re-running this
// * Consuming system resources: we were probably killed for resource conservation purposes
setIntentRedelivery(false);
public static Intent getFileCleanupIntent(ArrayList<String> filesToCleanup) {
Intent intent = new Intent();
intent.setAction(FileCleanupService.ACTION_DELETE_FILES);
intent.putExtra(FileCleanupService.EXTRA_FILE_PATHS_TO_DELETE, filesToCleanup);
return intent;
}
@Override
protected void onHandleIntent(final Intent intent) {
protected void onHandleWork(@NonNull final Intent intent) {
if (!isIntentValid(intent)) {
return;
}
@ -59,6 +64,14 @@ public class FileCleanupService extends IntentService {
}
}
@Override
public boolean onStopCurrentWork() {
// We're likely to get scheduled again - let's wait until then in order to avoid:
// * The coding complexity of re-running this
// * Consuming system resources: we were probably killed for resource conservation purposes
return false;
}
private static boolean isIntentValid(final Intent intent) {
if (intent == null) {
Log.w(LOGTAG, "Received null intent");