OSDN Git Service

Merge "Re-arrange camera initialization." 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.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;
32
33 import java.util.List;
34
35 /**
36  * Handles the picture resolution setting stored in SharedPreferences keyed by
37  * Keys.KEY_PICTURE_SIZE_BACK and Keys.KEY_PICTURE_SIZE_FRONT.
38  */
39 public class ResolutionSetting {
40     private static final Log.Tag TAG = new Log.Tag("ResolutionSettings");
41
42     private final SettingsManager mSettingsManager;
43     private final OneCameraManager mOneCameraManager;
44     private final String mResolutionBlackListBack;
45     private final String mResolutionBlackListFront;
46
47     public ResolutionSetting(SettingsManager settingsManager,
48             OneCameraManager oneCameraManager,
49             ContentResolver contentResolver) {
50         mSettingsManager = settingsManager;
51         mOneCameraManager = oneCameraManager;
52
53         mResolutionBlackListBack = GservicesHelper.getBlacklistedResolutionsBack(contentResolver);
54         mResolutionBlackListFront = GservicesHelper.getBlacklistedResolutionsFront(contentResolver);
55     }
56
57     /**
58      * Changes the picture size settings for the cameras with specified facing.
59      * Pick the largest picture size with the specified aspect ratio.
60      *
61      * @param cameraId The specific camera device.
62      * @param aspectRatio The chosen aspect ratio.
63      */
64     public void setPictureAspectRatio(CameraId cameraId, Rational aspectRatio)
65             throws OneCameraAccessException {
66         OneCameraCharacteristics cameraCharacteristics =
67                 mOneCameraManager.getOneCameraCharacteristics(cameraId);
68
69         Facing cameraFacing = cameraCharacteristics.getCameraDirection();
70
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;
77
78         // All resolutions supported by the camera.
79         List<Size> supportedPictureSizes = cameraCharacteristics
80                 .getSupportedPictureSizes(ImageFormat.JPEG);
81
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
84         // non-natively.
85         supportedPictureSizes = ResolutionUtil.getDisplayableSizesFromSupported(
86                 supportedPictureSizes, cameraFacing == OneCamera.Facing.BACK);
87
88         // Filter the remaining sizes through our backlist.
89         supportedPictureSizes = ResolutionUtil.filterBlackListedSizes(supportedPictureSizes,
90                 blacklist);
91
92         final Size chosenPictureSize =
93                 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
94         mSettingsManager.set(
95                 SettingsManager.SCOPE_GLOBAL,
96                 pictureSizeSettingKey,
97                 SettingsUtil.sizeToSettingString(chosenPictureSize));
98     }
99
100     /**
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.
104      */
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;
109
110         Size pictureSize = null;
111
112         String blacklist = "";
113         if (cameraFacing == OneCamera.Facing.BACK) {
114             blacklist = mResolutionBlackListBack;
115         } else if (cameraFacing == OneCamera.Facing.FRONT) {
116             blacklist = mResolutionBlackListFront;
117         }
118
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;
124
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);
132         }
133
134         if (!isPictureSizeSettingSet || isPictureSizeBlacklisted){
135             final Rational aspectRatio = ResolutionUtil.ASPECT_RATIO_4x3;
136
137             OneCameraCharacteristics cameraCharacteristics =
138                   mOneCameraManager.getOneCameraCharacteristics(cameraId);
139
140             final List<Size> supportedPictureSizes =
141                     ResolutionUtil.filterBlackListedSizes(
142                             cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG),
143                             blacklist);
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);
152         }
153         return pictureSize;
154     }
155
156     /**
157      * Obtains the preferred picture aspect ratio in terms of the picture size setting.
158      *
159      * @param cameraId The specific camera device.
160      * @return The preferred picture aspect ratio.
161      * @throws OneCameraAccessException
162      */
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());
167     }
168 }