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.exif.Rational;
24 import com.android.camera.one.OneCamera;
25 import com.android.camera.one.OneCameraAccessException;
26 import com.android.camera.one.OneCameraCharacteristics;
27 import com.android.camera.one.OneCameraManager;
28 import com.android.camera.util.GservicesHelper;
29 import com.android.camera.util.Size;
31 import java.util.List;
34 * Handles the picture resolution setting stored in SharedPreferences keyed by
35 * Keys.KEY_PICTURE_SIZE_BACK and Keys.KEY_PICTURE_SIZE_FRONT.
37 public class ResolutionSetting {
38 private static final Log.Tag TAG = new Log.Tag("ResolutionSettings");
40 private final SettingsManager mSettingsManager;
41 private final OneCameraManager mOneCameraManager;
42 private final String mResolutionBlackListBack;
43 private final String mResolutionBlackListFront;
45 public ResolutionSetting(SettingsManager settingsManager, OneCameraManager oneCameraManager,
46 ContentResolver contentResolver) {
47 mSettingsManager = settingsManager;
48 mOneCameraManager = oneCameraManager;
50 mResolutionBlackListBack = GservicesHelper.getBlacklistedResolutionsBack(contentResolver);
51 mResolutionBlackListFront = GservicesHelper.getBlacklistedResolutionsFront(contentResolver);
55 * Changes the picture size settings for the cameras with specified facing.
56 * Pick the largest picture size with the specified aspect ratio.
58 * @param cameraFacing The specified direction that the camera is facing.
59 * @param aspectRatio The chosen aspect ratio.
61 public void setPictureAspectRatio(OneCamera.Facing cameraFacing, Rational aspectRatio)
62 throws OneCameraAccessException {
63 OneCameraCharacteristics cameraCharacteristics =
64 mOneCameraManager.getCameraCharacteristics(cameraFacing);
66 // Pick the largest picture size with the selected aspect ratio and save
67 // the choice for front camera.
68 final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
69 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
70 final String blacklist = cameraFacing == OneCamera.Facing.FRONT ? mResolutionBlackListFront
71 : mResolutionBlackListBack;
73 // All resolutions supported by the camera.
74 List<Size> supportedPictureSizes = cameraCharacteristics
75 .getSupportedPictureSizes(ImageFormat.JPEG);
77 // Filter sizes which we are showing to the user in settings.
78 // This might also add some new resolution we support on some devices
80 supportedPictureSizes = ResolutionUtil.getDisplayableSizesFromSupported(
81 supportedPictureSizes, cameraFacing == OneCamera.Facing.BACK);
83 // Filter the remaining sizes through our backlist.
84 supportedPictureSizes = ResolutionUtil.filterBlackListedSizes(supportedPictureSizes,
87 final Size chosenPictureSize =
88 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
90 SettingsManager.SCOPE_GLOBAL,
91 pictureSizeSettingKey,
92 SettingsUtil.sizeToSettingString(chosenPictureSize));
96 * Reads the picture size setting for the cameras with specified facing.
98 * @param cameraFacing The specified direction that the camera is facing.
99 * @return The preferred picture size.
101 public Size getPictureSize(OneCamera.Facing cameraFacing) throws OneCameraAccessException {
102 final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
103 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
105 Size pictureSize = null;
107 String blacklist = "";
108 if (cameraFacing == OneCamera.Facing.BACK) {
109 blacklist = mResolutionBlackListBack;
110 } else if (cameraFacing == OneCamera.Facing.FRONT) {
111 blacklist = mResolutionBlackListFront;
114 // If there is no saved picture size preference or the saved on is
115 // blacklisted., pick a largest size with 4:3 aspect
116 boolean isPictureSizeSettingSet =
117 mSettingsManager.isSet(SettingsManager.SCOPE_GLOBAL, pictureSizeSettingKey);
118 boolean isPictureSizeBlacklisted = false;
120 // If a picture size is set, check whether it's blacklisted.
121 if (isPictureSizeSettingSet) {
122 pictureSize = SettingsUtil.sizeFromSettingString(
123 mSettingsManager.getString(SettingsManager.SCOPE_GLOBAL, pictureSizeSettingKey));
124 isPictureSizeBlacklisted = ResolutionUtil.isBlackListed(pictureSize, blacklist);
127 if (!isPictureSizeSettingSet || isPictureSizeBlacklisted){
128 final Rational aspectRatio = ResolutionUtil.ASPECT_RATIO_4x3;
130 final OneCameraCharacteristics cameraCharacteristics =
131 mOneCameraManager.getCameraCharacteristics(cameraFacing);
132 final List<Size> supportedPictureSizes =
133 ResolutionUtil.filterBlackListedSizes(
134 cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG),
136 final Size fallbackPictureSize =
137 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
138 mSettingsManager.set(
139 SettingsManager.SCOPE_GLOBAL,
140 pictureSizeSettingKey,
141 SettingsUtil.sizeToSettingString(fallbackPictureSize));
142 pictureSize = fallbackPictureSize;
143 Log.e(TAG, "Picture size setting is not set. Choose " + fallbackPictureSize);
149 * Obtains the preferred picture aspect ratio in terms of the picture size setting.
151 * @param cameraFacing The specified direction that the camera is facing.
152 * @return The preferred picture aspect ratio.
153 * @throws OneCameraAccessException
155 public Rational getPictureAspectRatio(OneCamera.Facing cameraFacing)
156 throws OneCameraAccessException {
157 Size pictureSize = getPictureSize(cameraFacing);
158 return new Rational(pictureSize.getWidth(), pictureSize.getHeight());