2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.settings;
19 import android.content.ContentResolver;
20 import android.graphics.ImageFormat;
22 import com.android.camera.debug.Log;
23 import com.android.camera.device.CameraId;
24 import com.android.camera.exif.Rational;
25 import com.android.camera.one.OneCamera;
26 import com.android.camera.one.OneCamera.Facing;
27 import com.android.camera.one.OneCameraAccessException;
28 import com.android.camera.one.OneCameraCharacteristics;
29 import com.android.camera.one.OneCameraManager;
30 import com.android.camera.util.GservicesHelper;
31 import com.android.camera.util.Size;
33 import java.util.List;
36 * Handles the picture resolution setting stored in SharedPreferences keyed by
37 * Keys.KEY_PICTURE_SIZE_BACK and Keys.KEY_PICTURE_SIZE_FRONT.
39 public class ResolutionSetting {
40 private static final Log.Tag TAG = new Log.Tag("ResolutionSettings");
42 private final SettingsManager mSettingsManager;
43 private final OneCameraManager mOneCameraManager;
44 private final String mResolutionBlackListBack;
45 private final String mResolutionBlackListFront;
47 public ResolutionSetting(SettingsManager settingsManager,
48 OneCameraManager oneCameraManager,
49 ContentResolver contentResolver) {
50 mSettingsManager = settingsManager;
51 mOneCameraManager = oneCameraManager;
53 mResolutionBlackListBack = GservicesHelper.getBlacklistedResolutionsBack(contentResolver);
54 mResolutionBlackListFront = GservicesHelper.getBlacklistedResolutionsFront(contentResolver);
58 * Changes the picture size settings for the cameras with specified facing.
59 * Pick the largest picture size with the specified aspect ratio.
61 * @param cameraId The specific camera device.
62 * @param aspectRatio The chosen aspect ratio.
64 public void setPictureAspectRatio(CameraId cameraId, Rational aspectRatio)
65 throws OneCameraAccessException {
66 OneCameraCharacteristics cameraCharacteristics =
67 mOneCameraManager.getOneCameraCharacteristics(cameraId);
69 Facing cameraFacing = cameraCharacteristics.getCameraDirection();
71 // Pick the largest picture size with the selected aspect ratio and save
72 // the choice for front camera.
73 final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
74 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
75 final String blacklist = cameraFacing == OneCamera.Facing.FRONT ? mResolutionBlackListFront
76 : mResolutionBlackListBack;
78 // All resolutions supported by the camera.
79 List<Size> supportedPictureSizes = cameraCharacteristics
80 .getSupportedPictureSizes(ImageFormat.JPEG);
82 // Filter sizes which we are showing to the user in settings.
83 // This might also add some new resolution we support on some devices
85 supportedPictureSizes = ResolutionUtil.getDisplayableSizesFromSupported(
86 supportedPictureSizes, cameraFacing == OneCamera.Facing.BACK);
88 // Filter the remaining sizes through our backlist.
89 supportedPictureSizes = ResolutionUtil.filterBlackListedSizes(supportedPictureSizes,
92 final Size chosenPictureSize =
93 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
95 SettingsManager.SCOPE_GLOBAL,
96 pictureSizeSettingKey,
97 SettingsUtil.sizeToSettingString(chosenPictureSize));
101 * Reads the picture size setting for the cameras with specified facing.
102 * This specifically avoids reading camera characteristics unless the size
103 * is blacklisted or is not cached to prevent a crash.
105 public Size getPictureSize(CameraId cameraId, Facing cameraFacing)
106 throws OneCameraAccessException {
107 final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
108 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
110 Size pictureSize = null;
112 String blacklist = "";
113 if (cameraFacing == OneCamera.Facing.BACK) {
114 blacklist = mResolutionBlackListBack;
115 } else if (cameraFacing == OneCamera.Facing.FRONT) {
116 blacklist = mResolutionBlackListFront;
119 // If there is no saved picture size preference or the saved on is
120 // blacklisted., pick a largest size with 4:3 aspect
121 boolean isPictureSizeSettingSet =
122 mSettingsManager.isSet(SettingsManager.SCOPE_GLOBAL, pictureSizeSettingKey);
123 boolean isPictureSizeBlacklisted = false;
125 // If a picture size is set, check whether it's blacklisted.
126 if (isPictureSizeSettingSet) {
127 pictureSize = SettingsUtil.sizeFromSettingString(
128 mSettingsManager.getString(SettingsManager.SCOPE_GLOBAL,
129 pictureSizeSettingKey));
130 isPictureSizeBlacklisted = pictureSize == null ||
131 ResolutionUtil.isBlackListed(pictureSize, blacklist);
134 if (!isPictureSizeSettingSet || isPictureSizeBlacklisted){
135 final Rational aspectRatio = ResolutionUtil.ASPECT_RATIO_4x3;
137 OneCameraCharacteristics cameraCharacteristics =
138 mOneCameraManager.getOneCameraCharacteristics(cameraId);
140 final List<Size> supportedPictureSizes =
141 ResolutionUtil.filterBlackListedSizes(
142 cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG),
144 final Size fallbackPictureSize =
145 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
146 mSettingsManager.set(
147 SettingsManager.SCOPE_GLOBAL,
148 pictureSizeSettingKey,
149 SettingsUtil.sizeToSettingString(fallbackPictureSize));
150 pictureSize = fallbackPictureSize;
151 Log.e(TAG, "Picture size setting is not set. Choose " + fallbackPictureSize);
157 * Obtains the preferred picture aspect ratio in terms of the picture size setting.
159 * @param cameraId The specific camera device.
160 * @return The preferred picture aspect ratio.
161 * @throws OneCameraAccessException
163 public Rational getPictureAspectRatio(CameraId cameraId, Facing facing)
164 throws OneCameraAccessException {
165 Size pictureSize = getPictureSize(cameraId, facing);
166 return new Rational(pictureSize.getWidth(), pictureSize.getHeight());