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.graphics.Rect; 22 import android.net.Uri; 23 import com.android.gallery3d.filtershow.cache.ImageLoader; 24 import com.android.gallery3d.filtershow.filters.FiltersManager; 25 import com.android.gallery3d.filtershow.tools.SaveImage; 26 27 import java.io.File; 28 29 public class ImageSavingTask extends ProcessingTask { 30 private ProcessingService mProcessingService; 31 32 static class SaveRequest implements Request { 33 Uri sourceUri; 34 Uri selectedUri; 35 File destinationFile; 36 ImagePreset preset; 37 boolean flatten; 38 int quality; 39 float sizeFactor; 40 Bitmap previewImage; 41 boolean exit; 42 } 43 44 static class UpdateBitmap implements Update { 45 Bitmap bitmap; 46 } 47 48 static class UpdateProgress implements Update { 49 int max; 50 int current; 51 } 52 53 static class UpdatePreviewSaved implements Update { 54 Uri uri; 55 boolean exit; 56 } 57 58 static class URIResult implements Result { 59 Uri uri; 60 boolean exit; 61 } 62 63 public ImageSavingTask(ProcessingService service) { 64 mProcessingService = service; 65 } 66 67 public void saveImage(Uri sourceUri, Uri selectedUri, 68 File destinationFile, ImagePreset preset, 69 Bitmap previewImage, boolean flatten, 70 int quality, float sizeFactor, boolean exit) { 71 SaveRequest request = new SaveRequest(); 72 request.sourceUri = sourceUri; 73 request.selectedUri = selectedUri; 74 request.destinationFile = destinationFile; 75 request.preset = preset; 76 request.flatten = flatten; 77 request.quality = quality; 78 request.sizeFactor = sizeFactor; 79 request.previewImage = previewImage; 80 request.exit = exit; 81 postRequest(request); 82 } 83 84 public Result doInBackground(Request message) { 85 SaveRequest request = (SaveRequest) message; 86 Uri sourceUri = request.sourceUri; 87 Uri selectedUri = request.selectedUri; 88 File destinationFile = request.destinationFile; 89 Bitmap previewImage = request.previewImage; 90 ImagePreset preset = request.preset; 91 boolean flatten = request.flatten; 92 final boolean exit = request.exit; 93 // We create a small bitmap showing the result that we can 94 // give to the notification 95 UpdateBitmap updateBitmap = new UpdateBitmap(); 96 updateBitmap.bitmap = createNotificationBitmap(previewImage, sourceUri, preset); 97 postUpdate(updateBitmap); 98 SaveImage saveImage = new SaveImage(mProcessingService, sourceUri, 99 selectedUri, destinationFile, previewImage, 100 new SaveImage.Callback() { 101 @Override 102 public void onPreviewSaved(Uri uri){ 103 UpdatePreviewSaved previewSaved = new UpdatePreviewSaved(); 104 previewSaved.uri = uri; 105 previewSaved.exit = exit; 106 postUpdate(previewSaved); 107 } 108 109 @Override 110 public void onProgress(int max, int current) { 111 UpdateProgress updateProgress = new UpdateProgress(); 112 updateProgress.max = max; 113 updateProgress.current = current; 114 postUpdate(updateProgress); 115 } 116 }); 117 Uri uri = saveImage.processAndSaveImage(preset, flatten, 118 request.quality, request.sizeFactor, request.exit); 119 URIResult result = new URIResult(); 120 result.uri = uri; 121 result.exit = request.exit; 122 return result; 123 } 124 125 @Override 126 public void onResult(Result message) { 127 URIResult result = (URIResult) message; 128 mProcessingService.completeSaveImage(result.uri, result.exit); 129 } 130 131 @Override 132 public void onUpdate(Update message) { 133 if (message instanceof UpdatePreviewSaved){ 134 Uri uri = ((UpdatePreviewSaved) message).uri; 135 boolean exit = ((UpdatePreviewSaved) message).exit; 136 mProcessingService.completePreviewSaveImage(uri, exit); 137 } 138 if (message instanceof UpdateBitmap) { 139 Bitmap bitmap = ((UpdateBitmap) message).bitmap; 140 mProcessingService.updateNotificationWithBitmap(bitmap); 141 } 142 if (message instanceof UpdateProgress) { 143 UpdateProgress progress = (UpdateProgress) message; 144 mProcessingService.updateProgress(progress.max, progress.current); 145 } 146 } 147 148 private Bitmap createNotificationBitmap(Bitmap preview, Uri sourceUri, ImagePreset preset) { 149 int notificationBitmapSize = Resources.getSystem().getDimensionPixelSize( 150 android.R.dimen.notification_large_icon_width); 151 if (preview != null) { 152 return Bitmap.createScaledBitmap(preview, 153 notificationBitmapSize, notificationBitmapSize, true); 154 } 155 Bitmap bitmap = ImageLoader.loadConstrainedBitmap(sourceUri, getContext(), 156 notificationBitmapSize, null, true); 157 CachingPipeline pipeline = new CachingPipeline(FiltersManager.getManager(), "Thumb"); 158 return pipeline.renderFinalImage(bitmap, preset); 159 } 160 161 } 162