summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/pipeline/ImageSavingTask.java
blob: e93ec168754de8cb8938ed1ab641fa7552677fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.pipeline;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import com.android.gallery3d.filtershow.cache.ImageLoader;
import com.android.gallery3d.filtershow.filters.FiltersManager;
import com.android.gallery3d.filtershow.tools.SaveImage;

import java.io.File;

public class ImageSavingTask extends ProcessingTask {
    private ProcessingService mProcessingService;

    static class SaveRequest implements Request {
        Uri sourceUri;
        Uri selectedUri;
        File destinationFile;
        ImagePreset preset;
    }

    static class UpdateBitmap implements Update {
        Bitmap bitmap;
    }

    static class UpdateProgress implements Update {
        int max;
        int current;
    }

    static class URIResult implements Result {
        Uri uri;
    }

    public ImageSavingTask(ProcessingService service) {
        mProcessingService = service;
    }

    public void saveImage(Uri sourceUri, Uri selectedUri,
                          File destinationFile, ImagePreset preset) {
        SaveRequest request = new SaveRequest();
        request.sourceUri = sourceUri;
        request.selectedUri = selectedUri;
        request.destinationFile = destinationFile;
        request.preset = preset;
        postRequest(request);
    }

    public Result doInBackground(Request message) {
        SaveRequest request = (SaveRequest) message;
        Uri sourceUri = request.sourceUri;
        Uri selectedUri = request.selectedUri;
        File destinationFile = request.destinationFile;
        ImagePreset preset = request.preset;

        // We create a small bitmap showing the result that we can
        // give to the notification
        UpdateBitmap updateBitmap = new UpdateBitmap();
        updateBitmap.bitmap = createNotificationBitmap(sourceUri, preset);
        postUpdate(updateBitmap);

        SaveImage saveImage = new SaveImage(mProcessingService, sourceUri,
                selectedUri, destinationFile,
                new SaveImage.Callback() {
                    @Override
                    public void onProgress(int max, int current) {
                        UpdateProgress updateProgress = new UpdateProgress();
                        updateProgress.max = max;
                        updateProgress.current = current;
                        postUpdate(updateProgress);
                    }
                });

        Uri uri = saveImage.processAndSaveImage(preset);
        URIResult result = new URIResult();
        result.uri = uri;
        return result;
    }

    @Override
    public void onResult(Result message) {
        URIResult result = (URIResult) message;
        mProcessingService.completeSaveImage(result.uri);
    }

    @Override
    public void onUpdate(Update message) {
        if (message instanceof UpdateBitmap) {
            Bitmap bitmap = ((UpdateBitmap) message).bitmap;
            mProcessingService.updateNotificationWithBitmap(bitmap);
        }
        if (message instanceof UpdateProgress) {
            UpdateProgress progress = (UpdateProgress) message;
            mProcessingService.updateProgress(progress.max, progress.current);
        }
    }

    private Bitmap createNotificationBitmap(Uri sourceUri, ImagePreset preset) {
        int notificationBitmapSize = Resources.getSystem().getDimensionPixelSize(
                android.R.dimen.notification_large_icon_width);
        Bitmap bitmap = ImageLoader.loadConstrainedBitmap(sourceUri, getContext(),
                notificationBitmapSize, null, true);
        CachingPipeline pipeline = new CachingPipeline(FiltersManager.getManager(), "Thumb");
        return pipeline.renderFinalImage(bitmap, preset);
    }

}