StackSaverImpl.java revision 24069e7a9cc9b4f908f18a71301285ccf5e164f6
1/*
2 * Copyright (C) 2014 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
17package com.android.camera.session;
18
19import android.content.ContentResolver;
20import android.location.Location;
21import android.net.Uri;
22
23import com.android.camera.Storage;
24import com.android.camera.debug.Log;
25import com.android.camera.exif.ExifInterface;
26
27import java.io.File;
28
29/**
30 * Default implementation of the {@link StackSaver} interface. It creates a
31 * directory for each stack and stores images and if needed also metadata inside
32 * that directory.
33 * <p>
34 * TODO: Add placeholder support for stack writing.
35 * </p>
36 */
37public class StackSaverImpl implements StackSaver {
38    private static final Log.Tag TAG = new Log.Tag("StackSaverImpl");
39    /** The stacked images are stored in this directory. */
40    private final File mStackDirectory;
41    private final ContentResolver mContentResolver;
42    private final Location mGpsLocation;
43
44    /**
45     * Instantiate a new stack saver implementation.
46     *
47     * @param stackDirectory the directory, which either exists already or can
48     *            be created, into which images belonging to this stack are
49     *            belonging.
50     * @param gpsLocation the GPS location to attach to all stacked images.
51     * @param contentResolver content resolver for storing the data in media
52     *            store. TODO: Replace with a media storage storer that can be
53     *            mocked out in tests.
54     */
55    public StackSaverImpl(File stackDirectory, Location gpsLocation, ContentResolver contentResolver) {
56        mStackDirectory = stackDirectory;
57        mGpsLocation = gpsLocation;
58        mContentResolver = contentResolver;
59    }
60
61    @Override
62    public Uri saveStackedImage(byte[] data, String title, int width, int height,
63            int imageOrientation, ExifInterface exif, long captureTimeEpoch, String mimeType) {
64        if (exif != null) {
65            exif.setTag(exif.buildTag(ExifInterface.TAG_ORIENTATION, imageOrientation));
66        }
67
68        String filePath =
69                Storage.generateFilepath(mStackDirectory.getAbsolutePath(), title, mimeType);
70        Log.d(TAG, "Saving using stack image saver: " + filePath);
71
72        long fileLength = Storage.writeFile(filePath, data, exif);
73        if (fileLength >= 0) {
74            return Storage.addImageToMediaStore(mContentResolver, title, captureTimeEpoch,
75                    mGpsLocation, imageOrientation, fileLength, filePath, width, height, mimeType);
76        }
77        return null;
78    }
79}
80