OSDN Git Service

Merge "Fix 16:9 start-up dialog selection." into ub-camera-haleakala
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / settings / ResolutionSetting.java
1 /*
2  * Copyright (C) 2015 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.ContentResolver;
20 import android.graphics.ImageFormat;
21
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;
30
31 import java.util.List;
32
33 /**
34  * Handles the picture resolution setting stored in SharedPreferences keyed by
35  * Keys.KEY_PICTURE_SIZE_BACK and Keys.KEY_PICTURE_SIZE_FRONT.
36  */
37 public class ResolutionSetting {
38     private static final Log.Tag TAG = new Log.Tag("ResolutionSettings");
39
40     private final SettingsManager mSettingsManager;
41     private final OneCameraManager mOneCameraManager;
42     private final String mResolutionBlackListBack;
43     private final String mResolutionBlackListFront;
44
45     public ResolutionSetting(SettingsManager settingsManager, OneCameraManager oneCameraManager,
46             ContentResolver contentResolver) {
47         mSettingsManager = settingsManager;
48         mOneCameraManager = oneCameraManager;
49
50         mResolutionBlackListBack = GservicesHelper.getBlacklistedResolutionsBack(contentResolver);
51         mResolutionBlackListFront = GservicesHelper.getBlacklistedResolutionsFront(contentResolver);
52     }
53
54     /**
55      * Changes the picture size settings for the cameras with specified facing.
56      * Pick the largest picture size with the specified aspect ratio.
57      *
58      * @param cameraFacing The specified direction that the camera is facing.
59      * @param aspectRatio The chosen aspect ratio.
60      */
61     public void setPictureAspectRatio(OneCamera.Facing cameraFacing, Rational aspectRatio)
62             throws OneCameraAccessException {
63         OneCameraCharacteristics cameraCharacteristics =
64                 mOneCameraManager.getCameraCharacteristics(cameraFacing);
65
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;
72
73         // All resolutions supported by the camera.
74         List<Size> supportedPictureSizes = cameraCharacteristics
75                 .getSupportedPictureSizes(ImageFormat.JPEG);
76
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
79         // non-natively.
80         supportedPictureSizes = ResolutionUtil.getDisplayableSizesFromSupported(
81                 supportedPictureSizes, cameraFacing == OneCamera.Facing.BACK);
82
83         // Filter the remaining sizes through our backlist.
84         supportedPictureSizes = ResolutionUtil.filterBlackListedSizes(supportedPictureSizes,
85                 blacklist);
86
87         final Size chosenPictureSize =
88                 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
89         mSettingsManager.set(
90                 SettingsManager.SCOPE_GLOBAL,
91                 pictureSizeSettingKey,
92                 SettingsUtil.sizeToSettingString(chosenPictureSize));
93     }
94
95     /**
96      * Reads the picture size setting for the cameras with specified facing.
97      *
98      * @param cameraFacing The specified direction that the camera is facing.
99      * @return The preferred picture size.
100      */
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;
104
105         Size pictureSize = null;
106
107         String blacklist = "";
108         if (cameraFacing == OneCamera.Facing.BACK) {
109             blacklist = mResolutionBlackListBack;
110         } else if (cameraFacing == OneCamera.Facing.FRONT) {
111             blacklist = mResolutionBlackListFront;
112         }
113
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;
119
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);
125         }
126
127         if (!isPictureSizeSettingSet || isPictureSizeBlacklisted){
128             final Rational aspectRatio = ResolutionUtil.ASPECT_RATIO_4x3;
129
130             final OneCameraCharacteristics cameraCharacteristics =
131                     mOneCameraManager.getCameraCharacteristics(cameraFacing);
132             final List<Size> supportedPictureSizes =
133                     ResolutionUtil.filterBlackListedSizes(
134                             cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG),
135                             blacklist);
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);
144         }
145         return pictureSize;
146     }
147
148     /**
149      * Obtains the preferred picture aspect ratio in terms of the picture size setting.
150      *
151      * @param cameraFacing The specified direction that the camera is facing.
152      * @return The preferred picture aspect ratio.
153      * @throws OneCameraAccessException
154      */
155     public Rational getPictureAspectRatio(OneCamera.Facing cameraFacing)
156             throws OneCameraAccessException {
157         Size pictureSize = getPictureSize(cameraFacing);
158         return new Rational(pictureSize.getWidth(), pictureSize.getHeight());
159     }
160 }