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