Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 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.camera; |
| 18 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 20 | import android.content.ContentValues; |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 21 | import android.graphics.BitmapFactory; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 22 | import android.location.Location; |
| 23 | import android.net.Uri; |
| 24 | import android.os.AsyncTask; |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 25 | import android.provider.MediaStore.Video; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 26 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 27 | import com.android.camera.app.MediaSaver; |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 28 | import com.android.camera.data.LocalData; |
Angus Kong | 5596b4c | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 29 | import com.android.camera.debug.Log; |
ztenghui | a16e7b5 | 2013-08-23 11:47:56 -0700 | [diff] [blame] | 30 | import com.android.camera.exif.ExifInterface; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 31 | |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 32 | import java.io.File; |
| 33 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 34 | /** |
| 35 | * A class implementing {@link com.android.camera.app.MediaSaver}. |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 36 | */ |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 37 | public class MediaSaverImpl implements MediaSaver { |
Angus Kong | 5596b4c | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 38 | private static final Log.Tag TAG = new Log.Tag("MediaSaverImpl"); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 39 | private static final String VIDEO_BASE_URI = "content://media/external/video/media"; |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 40 | |
Shashi Shekhar | 5777607 | 2014-11-26 17:55:52 -0800 | [diff] [blame] | 41 | /** The memory limit for unsaved image is 30MB. */ |
| 42 | // TODO: Revert this back to 20 MB when CaptureSession API supports saving |
| 43 | // bursts. |
| 44 | private static final int SAVE_TASK_MEMORY_LIMIT = 30 * 1024 * 1024; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 45 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 46 | private QueueListener mQueueListener; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 47 | |
| 48 | /** Memory used by the total queued save request, in bytes. */ |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 49 | private long mMemoryUse; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 50 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 51 | public MediaSaverImpl() { |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 52 | mMemoryUse = 0; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 55 | @Override |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 56 | public boolean isQueueFull() { |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 57 | return (mMemoryUse >= SAVE_TASK_MEMORY_LIMIT); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 60 | @Override |
| 61 | public void addImage(final byte[] data, String title, long date, Location loc, int width, |
| 62 | int height, int orientation, ExifInterface exif, OnMediaSavedListener l, |
| 63 | ContentResolver resolver) { |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 64 | addImage(data, title, date, loc, width, height, orientation, exif, l, |
| 65 | resolver, LocalData.MIME_TYPE_JPEG); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void addImage(final byte[] data, String title, long date, Location loc, int width, |
| 70 | int height, int orientation, ExifInterface exif, OnMediaSavedListener l, |
| 71 | ContentResolver resolver, String mimeType) { |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 72 | if (isQueueFull()) { |
| 73 | Log.e(TAG, "Cannot add image when the queue is full"); |
| 74 | return; |
| 75 | } |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 76 | ImageSaveTask t = new ImageSaveTask(data, title, date, |
| 77 | (loc == null) ? null : new Location(loc), |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 78 | width, height, orientation, mimeType, exif, resolver, l); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 79 | |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 80 | mMemoryUse += data.length; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 81 | if (isQueueFull()) { |
| 82 | onQueueFull(); |
| 83 | } |
| 84 | t.execute(); |
| 85 | } |
| 86 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 87 | @Override |
| 88 | public void addImage(final byte[] data, String title, long date, Location loc, int orientation, |
| 89 | ExifInterface exif, OnMediaSavedListener l, ContentResolver resolver) { |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 90 | // When dimensions are unknown, pass 0 as width and height, |
| 91 | // and decode image for width and height later in a background thread |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 92 | addImage(data, title, date, loc, 0, 0, orientation, exif, l, resolver, |
| 93 | LocalData.MIME_TYPE_JPEG); |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 94 | } |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public void addImage(final byte[] data, String title, Location loc, int width, int height, |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 97 | int orientation, ExifInterface exif, OnMediaSavedListener l, |
| 98 | ContentResolver resolver) { |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 99 | addImage(data, title, System.currentTimeMillis(), loc, width, height, |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 100 | orientation, exif, l, resolver, LocalData.MIME_TYPE_JPEG); |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 103 | @Override |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 104 | public void addVideo(String path, ContentValues values, OnMediaSavedListener l, |
| 105 | ContentResolver resolver) { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 106 | // We don't set a queue limit for video saving because the file |
| 107 | // is already in the storage. Only updating the database. |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 108 | new VideoSaveTask(path, values, l, resolver).execute(); |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public void setQueueListener(QueueListener l) { |
| 113 | mQueueListener = l; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 114 | if (l == null) { |
| 115 | return; |
| 116 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 117 | l.onQueueStatus(isQueueFull()); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void onQueueFull() { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 121 | if (mQueueListener != null) { |
| 122 | mQueueListener.onQueueStatus(true); |
| 123 | } |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private void onQueueAvailable() { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 127 | if (mQueueListener != null) { |
| 128 | mQueueListener.onQueueStatus(false); |
| 129 | } |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 132 | private class ImageSaveTask extends AsyncTask <Void, Void, Uri> { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 133 | private final byte[] data; |
| 134 | private final String title; |
| 135 | private final long date; |
| 136 | private final Location loc; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 137 | private int width, height; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 138 | private final int orientation; |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 139 | private final String mimeType; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 140 | private final ExifInterface exif; |
| 141 | private final ContentResolver resolver; |
| 142 | private final OnMediaSavedListener listener; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 143 | |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 144 | public ImageSaveTask(byte[] data, String title, long date, Location loc, |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 145 | int width, int height, int orientation, String mimeType, |
| 146 | ExifInterface exif, ContentResolver resolver, |
| 147 | OnMediaSavedListener listener) { |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 148 | this.data = data; |
| 149 | this.title = title; |
| 150 | this.date = date; |
| 151 | this.loc = loc; |
| 152 | this.width = width; |
| 153 | this.height = height; |
| 154 | this.orientation = orientation; |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 155 | this.mimeType = mimeType; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 156 | this.exif = exif; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 157 | this.resolver = resolver; |
| 158 | this.listener = listener; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected void onPreExecute() { |
| 163 | // do nothing. |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected Uri doInBackground(Void... v) { |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 168 | if (width == 0 || height == 0) { |
| 169 | // Decode bounds |
| 170 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 171 | options.inJustDecodeBounds = true; |
| 172 | BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 173 | width = options.outWidth; |
| 174 | height = options.outHeight; |
| 175 | } |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 176 | return Storage.addImage( |
Shashi Shekhar | 82d592f | 2014-11-12 09:52:19 -0800 | [diff] [blame] | 177 | resolver, title, date, loc, orientation, exif, data, width, height, |
| 178 | mimeType); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected void onPostExecute(Uri uri) { |
Angus Kong | c66ab6c | 2014-06-27 11:13:10 -0700 | [diff] [blame] | 183 | if (listener != null && uri != null) { |
| 184 | listener.onMediaSaved(uri); |
| 185 | } |
Angus Kong | c40c411 | 2013-07-17 15:59:03 -0700 | [diff] [blame] | 186 | boolean previouslyFull = isQueueFull(); |
| 187 | mMemoryUse -= data.length; |
Angus Kong | c66ab6c | 2014-06-27 11:13:10 -0700 | [diff] [blame] | 188 | if (isQueueFull() != previouslyFull) { |
| 189 | onQueueAvailable(); |
| 190 | } |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 193 | |
| 194 | private class VideoSaveTask extends AsyncTask <Void, Void, Uri> { |
| 195 | private String path; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 196 | private final ContentValues values; |
| 197 | private final OnMediaSavedListener listener; |
| 198 | private final ContentResolver resolver; |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 199 | |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 200 | public VideoSaveTask(String path, ContentValues values, OnMediaSavedListener l, |
| 201 | ContentResolver r) { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 202 | this.path = path; |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 203 | this.values = new ContentValues(values); |
| 204 | this.listener = l; |
| 205 | this.resolver = r; |
| 206 | } |
| 207 | |
| 208 | @Override |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 209 | protected Uri doInBackground(Void... v) { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 210 | Uri uri = null; |
| 211 | try { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 212 | Uri videoTable = Uri.parse(VIDEO_BASE_URI); |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 213 | uri = resolver.insert(videoTable, values); |
| 214 | |
| 215 | // Rename the video file to the final name. This avoids other |
| 216 | // apps reading incomplete data. We need to do it after we are |
| 217 | // certain that the previous insert to MediaProvider is completed. |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 218 | String finalName = values.getAsString(Video.Media.DATA); |
Andy Huibers | 6388e63 | 2014-04-03 22:32:46 -0700 | [diff] [blame] | 219 | File finalFile = new File(finalName); |
| 220 | if (new File(path).renameTo(finalFile)) { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 221 | path = finalName; |
| 222 | } |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 223 | resolver.update(uri, values, null, null); |
| 224 | } catch (Exception e) { |
| 225 | // We failed to insert into the database. This can happen if |
| 226 | // the SD card is unmounted. |
| 227 | Log.e(TAG, "failed to add video to media store", e); |
| 228 | uri = null; |
| 229 | } finally { |
| 230 | Log.v(TAG, "Current video URI: " + uri); |
| 231 | } |
| 232 | return uri; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | protected void onPostExecute(Uri uri) { |
Angus Kong | c66ab6c | 2014-06-27 11:13:10 -0700 | [diff] [blame] | 237 | if (listener != null) { |
| 238 | listener.onMediaSaved(uri); |
| 239 | } |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 242 | } |