OSDN Git Service

Merge "Add a shutdown mechanism to the camera lifecycle." 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 com.android.camera.debug.Log;
20 import com.android.camera.exif.Rational;
21 import com.android.camera.one.OneCamera;
22 import com.android.camera.one.OneCameraAccessException;
23 import com.android.camera.one.OneCameraCharacteristics;
24 import com.android.camera.one.OneCameraManager;
25 import com.android.camera.util.Size;
26
27 import android.graphics.ImageFormat;
28
29 import java.util.List;
30
31 /**
32  * Handles the picture resolution setting stored in SharedPreferences keyed by
33  * Keys.KEY_PICTURE_SIZE_BACK and Keys.KEY_PICTURE_SIZE_FRONT.
34  */
35 public class ResolutionSetting {
36     private static final Log.Tag TAG = new Log.Tag("ResolutionSettings");
37
38     private final SettingsManager mSettingsManager;
39
40     private final OneCameraManager mOneCameraManager;
41
42     public ResolutionSetting(SettingsManager settingsManager, OneCameraManager oneCameraManager) {
43         mSettingsManager = settingsManager;
44         mOneCameraManager = oneCameraManager;
45     }
46
47     /**
48      * Changes the picture size settings for the cameras with specified facing.
49      * Pick the largest picture size with the specified aspect ratio.
50      *
51      * @param cameraFacing The specified direction that the camera is facing.
52      * @param aspectRatio The chosen aspect ratio.
53      */
54     public void setPictureAspectRatio(OneCamera.Facing cameraFacing, Rational aspectRatio)
55             throws OneCameraAccessException {
56         OneCameraCharacteristics cameraCharacteristics =
57                 mOneCameraManager.getCameraCharacteristics(cameraFacing);
58
59         // Pick the largest picture size with the selected aspect ratio and save the choice for front camera.
60         final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
61                 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
62         final List<Size> supportedPictureSizes =
63                 cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG);
64         final Size chosenPictureSize =
65                 ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
66         mSettingsManager.set(
67                 SettingsManager.SCOPE_GLOBAL,
68                 pictureSizeSettingKey,
69                 SettingsUtil.sizeToSettingString(chosenPictureSize));
70     }
71
72     /**
73      * Reads the picture size setting for the cameras with specified facing.
74      *
75      * @param cameraFacing The specified direction that the camera is facing.
76      * @return The preferred picture size.
77      */
78     public Size getPictureSize(OneCamera.Facing cameraFacing) throws OneCameraAccessException {
79         final String pictureSizeSettingKey = cameraFacing == OneCamera.Facing.FRONT ?
80                 Keys.KEY_PICTURE_SIZE_FRONT : Keys.KEY_PICTURE_SIZE_BACK;
81
82         /**
83          * If there is no saved reference, pick a largest size with 4:3 aspect
84          * ratio as a fallback.
85          */
86         final boolean isPictureSizeSettingSet =
87                 mSettingsManager.isSet(SettingsManager.SCOPE_GLOBAL, pictureSizeSettingKey);
88         if (!isPictureSizeSettingSet) {
89             final Rational aspectRatio = ResolutionUtil.ASPECT_RATIO_4x3;
90
91             final OneCameraCharacteristics cameraCharacteristics =
92                     mOneCameraManager.getCameraCharacteristics(cameraFacing);
93             final List<Size> supportedPictureSizes =
94                     cameraCharacteristics.getSupportedPictureSizes(ImageFormat.JPEG);
95             final Size fallbackPictureSize =
96                     ResolutionUtil.getLargestPictureSize(aspectRatio, supportedPictureSizes);
97             mSettingsManager.set(
98                     SettingsManager.SCOPE_GLOBAL,
99                     pictureSizeSettingKey,
100                     SettingsUtil.sizeToSettingString(fallbackPictureSize));
101             Log.e(TAG, "Picture size setting is not set. Choose " + fallbackPictureSize);
102         }
103
104         /** Reads picture size setting from SettingsManager. */
105         return SettingsUtil.sizeFromSettingString(
106                 mSettingsManager.getString(SettingsManager.SCOPE_GLOBAL, pictureSizeSettingKey));
107     }
108
109     /**
110      * Obtains the preferred picture aspect ratio in terms of the picture size setting.
111      *
112      * @param cameraFacing The specified direction that the camera is facing.
113      * @return The preferred picture aspect ratio.
114      * @throws OneCameraAccessException
115      */
116     public Rational getPictureAspectRatio(OneCamera.Facing cameraFacing)
117             throws OneCameraAccessException {
118         Size pictureSize = getPictureSize(cameraFacing);
119         return new Rational(pictureSize.getWidth(), pictureSize.getHeight());
120     }
121 }