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.ex.camera2.portability.Size;
26 
27 import java.util.List;
28 
29 /**
30  * Facilitates caching of camera supported picture sizes, which is slow
31  * to query.  Will update cache if Build ID changes.
32  */
33 public class CameraPictureSizesCacher {
34     public static final String PICTURE_SIZES_BUILD_KEY = "CachedSupportedPictureSizes_Build_Camera";
35     public static final String PICTURE_SIZES_SIZES_KEY = "CachedSupportedPictureSizes_Sizes_Camera";
36 
37     /**
38      * Opportunistically update the picture sizes cache, if needed.
39      *
40      * @param cameraId cameraID we have sizes for.
41      * @param sizes List of valid sizes.
42      */
updateSizesForCamera(Context context, int cameraId, List<Size> sizes)43     public static void updateSizesForCamera(Context context, int cameraId, List<Size> sizes) {
44         String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
45         SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
46         String thisCameraCachedBuild = defaultPrefs.getString(key_build, null);
47         // Write to cache.
48         if (thisCameraCachedBuild == null) {
49             String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
50             SharedPreferences.Editor editor = defaultPrefs.edit();
51             editor.putString(key_build, Build.DISPLAY);
52             editor.putString(key_sizes, Size.listToString(sizes));
53             editor.apply();
54         }
55     }
56 
57     /**
58      * Return list of Sizes for provided cameraId.  Check first to see if we
59      * have it in the cache for the current android.os.Build.
60      * Note: This method calls Camera.open(), so the camera must be closed
61      * before calling or null will be returned if sizes were not previously
62      * cached.
63      *
64      * @param cameraId cameraID we would like sizes for.
65      * @return List of valid sizes, or null if the Camera can not be opened.
66      */
getSizesForCamera(int cameraId, Context context)67     public static List<Size> getSizesForCamera(int cameraId, Context context) {
68         String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
69         String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
70         SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
71         // Return cached value for cameraId and current build, if available.
72         String thisCameraCachedBuild = defaultPrefs.getString(key_build, null);
73         if (thisCameraCachedBuild != null && thisCameraCachedBuild.equals(Build.DISPLAY)) {
74             String thisCameraCachedSizeList = defaultPrefs.getString(key_sizes, null);
75             if (thisCameraCachedSizeList != null) {
76                 return Size.stringToList(thisCameraCachedSizeList);
77             }
78         }
79         // No cached value, so need to query Camera API.
80         Camera thisCamera;
81         try {
82             thisCamera = Camera.open(cameraId);
83         } catch (RuntimeException e) {
84             // Camera open will fail if already open.
85             return null;
86         }
87         if (thisCamera != null) {
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