1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.work.integration.testapp.imageprocessing;
18
19import android.support.annotation.NonNull;
20import android.text.TextUtils;
21import android.util.Log;
22
23import androidx.work.Worker;
24import androidx.work.integration.testapp.db.Image;
25import androidx.work.integration.testapp.db.TestDatabase;
26
27import java.io.File;
28import java.util.List;
29
30/**
31 * Removes all existing {@link Image} entities and deletes associated compressed files
32 */
33public class ImageCleanupWorker extends Worker {
34    private static final String TAG = "ImageProcessingWorker";
35
36    @Override
37    public @NonNull Result doWork() {
38        Log.d(TAG, "Started");
39        List<Image> images = TestDatabase.getInstance(getApplicationContext())
40                .getImageDao().getImages();
41        for (Image image : images) {
42            if (!TextUtils.isEmpty(image.mProcessedFilePath)) {
43                if (new File(image.mProcessedFilePath).delete()) {
44                    Log.d(TAG, "Deleted : " + image.mProcessedFilePath);
45                } else {
46                    Log.e(TAG, "Failed to delete : " + image.mProcessedFilePath);
47                }
48            } else {
49                Log.d(TAG, image.mOriginalAssetName + "was not processed");
50            }
51        }
52        TestDatabase.getInstance(getApplicationContext()).getImageDao().clear();
53        Log.d(TAG, "Cleanup Complete!");
54        return Result.SUCCESS;
55    }
56}
57