/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.camera; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.media.CamcorderProfile; import android.hardware.Camera.Parameters; import android.hardware.Camera.Size; import android.preference.PreferenceManager; import android.util.Log; import com.google.android.camera.R; import java.util.ArrayList; import java.util.List; /** * Provides utilities and keys for Camera settings. */ public class CameraSettings { private static final int NOT_FOUND = -1; public static final String KEY_VERSION = "pref_version_key"; public static final String KEY_RECORD_LOCATION = RecordLocationPreference.KEY; public static final String KEY_VIDEO_QUALITY = "pref_camera_videoquality_key"; public static final String KEY_VIDEO_DURATION = "pref_camera_video_duration_key"; public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key"; public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key"; public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key"; public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; public static final String KEY_QUICK_CAPTURE = "pref_camera_quickcapture_key"; public static final String QUICK_CAPTURE_ON = "on"; public static final String QUICK_CAPTURE_OFF = "off"; public static final int CURRENT_VERSION = 3; // max mms video duration in seconds. public static final int MMS_VIDEO_DURATION = CamcorderProfile.getMmsRecordingDurationInSeconds(); public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; // MMS video length public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; @SuppressWarnings("unused") private static final String TAG = "CameraSettings"; private final Context mContext; private final Parameters mParameters; public CameraSettings(Activity activity, Parameters parameters) { mContext = activity; mParameters = parameters; } public PreferenceGroup getPreferenceGroup(int preferenceRes) { PreferenceInflater inflater = new PreferenceInflater(mContext); PreferenceGroup group = (PreferenceGroup) inflater.inflate(preferenceRes); initPreference(group); return group; } public static void initialCameraPictureSize( Context context, Parameters parameters) { // When launching the camera app first time, we will set the picture // size to the first one in the list defined in "arrays.xml" and is also // supported by the driver. List supported = parameters.getSupportedPictureSizes(); if (supported == null) return; for (String candidate : context.getResources().getStringArray( R.array.pref_camera_picturesize_entryvalues)) { if (setCameraPictureSize(candidate, supported, parameters)) { SharedPreferences.Editor editor = PreferenceManager .getDefaultSharedPreferences(context).edit(); editor.putString(KEY_PICTURE_SIZE, candidate); editor.commit(); return; } } Log.e(TAG, "No supported picture size found"); } public static void removePreferenceFromScreen( PreferenceGroup group, String key) { removePreference(group, key); } public static boolean setCameraPictureSize( String candidate, List supported, Parameters parameters) { int index = candidate.indexOf('x'); if (index == NOT_FOUND) return false; int width = Integer.parseInt(candidate.substring(0, index)); int height = Integer.parseInt(candidate.substring(index + 1)); for (Size size: supported) { if (size.width == width && size.height == height) { parameters.setPictureSize(width, height); return true; } } return false; } private void initPreference(PreferenceGroup group) { ListPreference videoDuration = group.findPreference(KEY_VIDEO_DURATION); ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE); ListPreference whiteBalance = group.findPreference(KEY_WHITE_BALANCE); ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT); ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE); ListPreference flashMode = group.findPreference(KEY_FLASH_MODE); ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE); // Since the screen could be loaded from different resources, we need // to check if the preference is available here if (videoDuration != null) { // Modify video duration settings. // The first entry is for MMS video duration, and we need to fill // in the device-dependent value (in seconds). CharSequence[] entries = videoDuration.getEntries(); entries[0] = String.format( entries[0].toString(), MMS_VIDEO_DURATION); } // Filter out unsupported settings / options if (pictureSize != null) { filterUnsupportedOptions(group, pictureSize, sizeListToStringList( mParameters.getSupportedPictureSizes())); } if (whiteBalance != null) { filterUnsupportedOptions(group, whiteBalance, mParameters.getSupportedWhiteBalance()); } if (colorEffect != null) { filterUnsupportedOptions(group, colorEffect, mParameters.getSupportedColorEffects()); } if (sceneMode != null) { filterUnsupportedOptions(group, sceneMode, mParameters.getSupportedSceneModes()); } if (flashMode != null) { filterUnsupportedOptions(group, flashMode, mParameters.getSupportedFlashModes()); } if (focusMode != null) { filterUnsupportedOptions(group, focusMode, mParameters.getSupportedFocusModes()); } } private static boolean removePreference(PreferenceGroup group, String key) { for (int i = 0, n = group.size(); i < n; i++) { CameraPreference child = group.get(i); if (child instanceof PreferenceGroup) { if (removePreference((PreferenceGroup) child, key)) { return true; } } if (child instanceof ListPreference && ((ListPreference) child).getKey().equals(key)) { group.removePreference(i); return true; } } return false; } private void filterUnsupportedOptions(PreferenceGroup group, ListPreference pref, List supported) { CharSequence[] allEntries = pref.getEntries(); // Remove the preference if the parameter is not supported or there is // only one options for the settings. if (supported == null || supported.size() <= 1) { removePreference(group, pref.getKey()); return; } CharSequence[] allEntryValues = pref.getEntryValues(); Drawable[] allIcons = (pref instanceof IconListPreference) ? ((IconListPreference) pref).getIcons() : null; ArrayList entries = new ArrayList(); ArrayList entryValues = new ArrayList(); ArrayList icons = allIcons == null ? null : new ArrayList(); for (int i = 0, len = allEntryValues.length; i < len; i++) { if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { entries.add(allEntries[i]); entryValues.add(allEntryValues[i]); if (allIcons != null) icons.add(allIcons[i]); } } // Set entries and entry values to list preference. int size = entries.size(); pref.setEntries(entries.toArray(new CharSequence[size])); pref.setEntryValues(entryValues.toArray(new CharSequence[size])); if (allIcons != null) { ((IconListPreference) pref) .setIcons(icons.toArray(new Drawable[size])); } // Set the value to the first entry if it is invalid. String value = pref.getValue(); if (pref.findIndexOfValue(value) == NOT_FOUND) { pref.setValueIndex(0); } } private static List sizeListToStringList(List sizes) { ArrayList list = new ArrayList(); for (Size size : sizes) { list.add(String.format("%dx%d", size.width, size.height)); } return list; } public static void upgradePreferences(SharedPreferences pref) { int version; try { version = pref.getInt(KEY_VERSION, 0); } catch (Exception ex) { version = 0; } if (version == CURRENT_VERSION) return; SharedPreferences.Editor editor = pref.edit(); if (version == 0) { // For old version, change 1 to 10 for video duration preference. if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { editor.putString(KEY_VIDEO_DURATION, "10"); } version = 1; } if (version == 1) { // Change jpeg quality {65,75,85} to {normal,fine,superfine} String quality = pref.getString(KEY_JPEG_QUALITY, "85"); if (quality.equals("65")) { quality = "normal"; } else if (quality.equals("75")) { quality = "fine"; } else { quality = "superfine"; } editor.putString(KEY_JPEG_QUALITY, quality); version = 2; } if (version == 2) { editor.putString(KEY_RECORD_LOCATION, pref.getBoolean(KEY_RECORD_LOCATION, false) ? RecordLocationPreference.VALUE_ON : RecordLocationPreference.VALUE_NONE); version = 3; } editor.putInt(KEY_VERSION, CURRENT_VERSION); editor.commit(); } }