2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.pipeline;
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;
28 public class ImageSavingTask extends ProcessingTask {
29 private ProcessingService mProcessingService;
31 static class SaveRequest implements Request {
38 static class UpdateBitmap implements Update {
42 static class UpdateProgress implements Update {
47 static class URIResult implements Result {
51 public ImageSavingTask(ProcessingService service) {
52 mProcessingService = service;
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;
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;
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);
78 SaveImage saveImage = new SaveImage(mProcessingService, sourceUri,
79 selectedUri, destinationFile,
80 new SaveImage.Callback() {
82 public void onProgress(int max, int current) {
83 UpdateProgress updateProgress = new UpdateProgress();
84 updateProgress.max = max;
85 updateProgress.current = current;
86 postUpdate(updateProgress);
90 Uri uri = saveImage.processAndSaveImage(preset);
91 URIResult result = new URIResult();
97 public void onResult(Result message) {
98 URIResult result = (URIResult) message;
99 mProcessingService.completeSaveImage(result.uri);
103 public void onUpdate(Update message) {
104 if (message instanceof UpdateBitmap) {
105 Bitmap bitmap = ((UpdateBitmap) message).bitmap;
106 mProcessingService.updateNotificationWithBitmap(bitmap);
108 if (message instanceof UpdateProgress) {
109 UpdateProgress progress = (UpdateProgress) message;
110 mProcessingService.updateProgress(progress.max, progress.current);
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);