Home | History | Annotate | Download | only in settings
      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.settings;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.hardware.Camera;
     22 import android.os.Build;
     23 import android.preference.PreferenceManager;
     24 
     25 import com.android.camera.util.Size;
     26 import com.google.common.base.Optional;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * Facilitates caching of camera supported picture sizes, which is slow
     32  * to query.  Will update cache if Build ID changes.
     33  */
     34 public class CameraPictureSizesCacher {
     35     private static final String PICTURE_SIZES_BUILD_KEY = "CachedSupportedPictureSizes_Build_Camera";
     36     private static final String PICTURE_SIZES_SIZES_KEY = "CachedSupportedPictureSizes_Sizes_Camera";
     37 
     38     /**
     39      * Opportunistically update the picture sizes cache, if needed.
     40      *
     41      * @param cameraId cameraID we have sizes for.
     42      * @param sizes List of valid sizes.
     43      */
     44     public static void updateSizesForCamera(Context context, int cameraId, List<Size> sizes) {
     45         String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
     46         SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
     47         String thisCameraCachedBuild = defaultPrefs.getString(key_build, null);
     48         // Write to cache.
     49         if (thisCameraCachedBuild == null) {
     50             String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
     51             SharedPreferences.Editor editor = defaultPrefs.edit();
     52             editor.putString(key_build, Build.DISPLAY);
     53             editor.putString(key_sizes, Size.listToString(sizes));
     54             editor.apply();
     55         }
     56     }
     57 
     58     /**
     59      * Return list of Sizes for provided cameraId.  Check first to see if we
     60      * have it in the cache for the current android.os.Build.
     61      * Note: This method calls Camera.open(), so the camera must be closed
     62      * before calling or null will be returned if sizes were not previously
     63      * cached.
     64      *
     65      * @param cameraId cameraID we would like sizes for.
     66      * @param context valid android application context.
     67      * @return List of valid sizes, or null if the Camera can not be opened.
     68      */
     69     public static List<Size> getSizesForCamera(int cameraId, Context context) {
     70         Optional<List<Size>> cachedSizes = getCachedSizesForCamera(cameraId, context);
     71         if (cachedSizes.isPresent()) {
     72             return cachedSizes.get();
     73         }
     74 
     75         // No cached value, so need to query Camera API.
     76         Camera thisCamera;
     77         try {
     78             thisCamera = Camera.open(cameraId);
     79         } catch (RuntimeException e) {
     80             // Camera open will fail if already open.
     81             return null;
     82         }
     83         if (thisCamera != null) {
     84             String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
     85             String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
     86             SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
     87 
     88             List<Size> sizes = Size.buildListFromCameraSizes(thisCamera.getParameters()
     89                     .getSupportedPictureSizes());
     90             thisCamera.release();
     91             SharedPreferences.Editor editor = defaultPrefs.edit();
     92             editor.putString(key_build, Build.DISPLAY);
     93             editor.putString(key_sizes, Size.listToString(sizes));
     94             editor.apply();
     95             return sizes;
     96         }
     97         return null;
     98     }
     99 
    100     /**
    101      * Returns the cached sizes for the current camera. See
    102      * {@link #getSizesForCamera} for details.
    103      *
    104      * @param cameraId cameraID we would like sizes for.
    105      * @param context valid android application context.
    106      * @return Optional ist of valid sizes. Not present if the sizes for the
    107      *         given camera were not cached.
    108      */
    109     public static Optional<List<Size>> getCachedSizesForCamera(int cameraId, Context context) {
    110         String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
    111         String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
    112         SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    113         // Return cached value for cameraId and current build, if available.
    114         String thisCameraCachedBuild = defaultPrefs.getString(key_build, null);
    115         if (thisCameraCachedBuild != null && thisCameraCachedBuild.equals(Build.DISPLAY)) {
    116             String thisCameraCachedSizeList = defaultPrefs.getString(key_sizes, null);
    117             if (thisCameraCachedSizeList != null) {
    118                 return Optional.of(Size.stringToList(thisCameraCachedSizeList));
    119             }
    120         }
    121         return Optional.absent();
    122     }
    123 }
    124