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