OSDN Git Service

Add background processing service
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / ImageSavingTask.java
1 /*
2  * Copyright (C) 2013 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
17 package com.android.gallery3d.filtershow.pipeline;
18
19 import android.content.res.Resources;
20 import android.graphics.Bitmap;
21 import android.net.Uri;
22 import com.android.gallery3d.filtershow.cache.ImageLoader;
23 import com.android.gallery3d.filtershow.filters.FiltersManager;
24 import com.android.gallery3d.filtershow.tools.SaveImage;
25
26 import java.io.File;
27
28 public class ImageSavingTask extends ProcessingTask {
29     private ProcessingService mProcessingService;
30
31     static class SaveRequest implements Request {
32         Uri sourceUri;
33         Uri selectedUri;
34         File destinationFile;
35         ImagePreset preset;
36     }
37
38     static class UpdateBitmap implements Update {
39         Bitmap bitmap;
40     }
41
42     static class UpdateProgress implements Update {
43         int max;
44         int current;
45     }
46
47     static class URIResult implements Result {
48         Uri uri;
49     }
50
51     public ImageSavingTask(ProcessingService service) {
52         mProcessingService = service;
53     }
54
55     public void saveImage(Uri sourceUri, Uri selectedUri,
56                           File destinationFile, ImagePreset preset) {
57         SaveRequest request = new SaveRequest();
58         request.sourceUri = sourceUri;
59         request.selectedUri = selectedUri;
60         request.destinationFile = destinationFile;
61         request.preset = preset;
62         postRequest(request);
63     }
64
65     public Result doInBackground(Request message) {
66         SaveRequest request = (SaveRequest) message;
67         Uri sourceUri = request.sourceUri;
68         Uri selectedUri = request.selectedUri;
69         File destinationFile = request.destinationFile;
70         ImagePreset preset = request.preset;
71
72         // We create a small bitmap showing the result that we can
73         // give to the notification
74         UpdateBitmap updateBitmap = new UpdateBitmap();
75         updateBitmap.bitmap = createNotificationBitmap(sourceUri, preset);
76         postUpdate(updateBitmap);
77
78         SaveImage saveImage = new SaveImage(mProcessingService, sourceUri,
79                 selectedUri, destinationFile,
80                 new SaveImage.Callback() {
81                     @Override
82                     public void onProgress(int max, int current) {
83                         UpdateProgress updateProgress = new UpdateProgress();
84                         updateProgress.max = max;
85                         updateProgress.current = current;
86                         postUpdate(updateProgress);
87                     }
88                 });
89
90         Uri uri = saveImage.processAndSaveImage(preset);
91         URIResult result = new URIResult();
92         result.uri = uri;
93         return result;
94     }
95
96     @Override
97     public void onResult(Result message) {
98         URIResult result = (URIResult) message;
99         mProcessingService.completeSaveImage(result.uri);
100     }
101
102     @Override
103     public void onUpdate(Update message) {
104         if (message instanceof UpdateBitmap) {
105             Bitmap bitmap = ((UpdateBitmap) message).bitmap;
106             mProcessingService.updateNotificationWithBitmap(bitmap);
107         }
108         if (message instanceof UpdateProgress) {
109             UpdateProgress progress = (UpdateProgress) message;
110             mProcessingService.updateProgress(progress.max, progress.current);
111         }
112     }
113
114     private Bitmap createNotificationBitmap(Uri sourceUri, ImagePreset preset) {
115         int notificationBitmapSize = Resources.getSystem().getDimensionPixelSize(
116                 android.R.dimen.notification_large_icon_width);
117         Bitmap bitmap = ImageLoader.loadConstrainedBitmap(sourceUri, getContext(),
118                 notificationBitmapSize, null, true);
119         CachingPipeline pipeline = new CachingPipeline(FiltersManager.getManager(), "Thumb");
120         return pipeline.renderFinalImage(bitmap, preset);
121     }
122
123 }