diff options
author | Chung-yih Wang <cywang@google.com> | 2010-11-22 13:46:30 +0800 |
---|---|---|
committer | Chung-yih Wang <cywang@google.com> | 2010-11-25 19:07:04 +0800 |
commit | 59ddc8c1add2fcffe523e3877baad1b6cc5ba2fb (patch) | |
tree | 9ef281563deca6c36963757340a33df46b0449bc /src | |
parent | 623fc04dd6194559a22b8cc9305473bf9205eb58 (diff) | |
download | LegacyCamera-59ddc8c1add2fcffe523e3877baad1b6cc5ba2fb.zip LegacyCamera-59ddc8c1add2fcffe523e3877baad1b6cc5ba2fb.tar.gz LegacyCamera-59ddc8c1add2fcffe523e3877baad1b6cc5ba2fb.tar.bz2 |
Add front/back camera switch
bug:3156676
Change-Id: I8a5394aefa068cb099b89c5739abf0cfd78591a9
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/camera/Camera.java | 53 | ||||
-rw-r--r-- | src/com/android/camera/CameraSettings.java | 45 | ||||
-rw-r--r-- | src/com/android/camera/PreviewFrameLayout.java | 18 | ||||
-rw-r--r-- | src/com/android/camera/VideoCamera.java | 57 | ||||
-rw-r--r-- | src/com/android/camera/ui/CameraPicker.java | 87 |
5 files changed, 214 insertions, 46 deletions
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java index 0396a07..2cf81d8 100644 --- a/src/com/android/camera/Camera.java +++ b/src/com/android/camera/Camera.java @@ -19,6 +19,7 @@ package com.android.camera; import com.android.camera.gallery.IImage; import com.android.camera.gallery.IImageList; import com.android.camera.ui.CameraHeadUpDisplay; +import com.android.camera.ui.CameraPicker; import com.android.camera.ui.GLRootView; import com.android.camera.ui.HeadUpDisplay; import com.android.camera.ui.ControlPanel; @@ -177,6 +178,9 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, private View mGpsNoSignalView; private View mGpsHasSignalView; + // Front/Back camera pciker for xlarge layout + private CameraPicker mCameraPicker; + /** * An unpublished intent flag requesting to return as soon as capturing * is completed. @@ -245,6 +249,8 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, // multiple cameras support private int mNumberOfCameras; private int mCameraId; + private int mFrontCameraId; + private int mBackCameraId; private boolean mQuickCapture; @@ -582,6 +588,18 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, } }; + private void initializeCameraPicker() { + mCameraPicker = (CameraPicker) findViewById(R.id.camera_picker); + if (mCameraPicker != null) { + ListPreference pref = mPreferenceGroup.findPreference( + CameraSettings.KEY_CAMERA_ID); + if (pref != null) { + mCameraPicker.initialize(pref); + mCameraPicker.setListener(new MyCameraPickerListener()); + } + } + } + private void initGpsOnScreenIndicator() { mGpsNoSignalView = findViewById(R.id.onscreen_gps_indicator_no_signal); mGpsHasSignalView = findViewById(R.id.onscreen_gps_indicator_on); @@ -1055,9 +1073,12 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, // ignore } + loadCameraPreferences(); + // Do this after starting preview because it depends on camera // parameters. initializeControlPanel(); + initializeCameraPicker(); } private void changeHeadUpDisplayState() { @@ -1095,6 +1116,17 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, } } + private void loadCameraPreferences() { + CameraSettings settings = new CameraSettings(this, mInitialParams, + mCameraId, CameraHolder.instance().getCameraInfo()); + mPreferenceGroup = settings.getPreferenceGroup( + R.xml.camera_preferences); + mFrontCameraId = + settings.getCameraIdByIndex(CameraInfo.CAMERA_FACING_FRONT); + mBackCameraId = + settings.getCameraIdByIndex(CameraInfo.CAMERA_FACING_BACK); + } + private void initializeControlPanel() { String[] keys = new String[]{CameraSettings.KEY_FLASH_MODE, CameraSettings.KEY_WHITE_BALANCE, @@ -1102,9 +1134,6 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, CameraSettings.KEY_CAMERA_ID}; mControlPanel = (ControlPanel) findViewById(R.id.control_panel); if (mControlPanel != null) { - CameraSettings settings = new CameraSettings(this, mInitialParams, - mCameraId, CameraHolder.instance().getCameraInfo()); - mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences); mControlPanel.initialize(this, mPreferenceGroup, keys, true); mControlPanel.setListener(new MyControlPanelListener()); mPopupGestureDetector = new GestureDetector(this, @@ -1114,13 +1143,10 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, private void initializeHeadUpDisplay() { if (mHeadUpDisplay == null) return; - CameraSettings settings = new CameraSettings(this, mInitialParams, - mCameraId, CameraHolder.instance().getCameraInfo()); // If we have zoom picker, do not show zoom control on head-up display. float[] zoomRatios = null; if (mZoomPicker == null) zoomRatios = getZoomRatios(); - mHeadUpDisplay.initialize(this, - settings.getPreferenceGroup(R.xml.camera_preferences), + mHeadUpDisplay.initialize(this, mPreferenceGroup, zoomRatios, mOrientationCompensation); if (mZoomPicker == null && mParameters.isZoomSupported()) { mHeadUpDisplay.setZoomListener(new ZoomControllerListener() { @@ -2192,7 +2218,10 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, R.string.switch_camera_id) .setOnMenuItemClickListener(new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { - switchCameraId((mCameraId + 1) % mNumberOfCameras); + CameraSettings.writePreferredCameraId(mPreferences, + ((mCameraId == mFrontCameraId) + ? mBackCameraId : mFrontCameraId)); + onSharedPreferenceChanged(); return true; } }).setIcon(android.R.drawable.ic_menu_camera); @@ -2202,7 +2231,6 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, private void switchCameraId(int cameraId) { if (mPausing || !isCameraIdle()) return; mCameraId = cameraId; - CameraSettings.writePreferredCameraId(mPreferences, cameraId); stopPreview(); closeCamera(); @@ -2323,5 +2351,10 @@ public class Camera extends NoSearchActivity implements View.OnClickListener, Camera.this.onSharedPreferenceChanged(); } } -} + private class MyCameraPickerListener implements CameraPicker.Listener { + public void onSharedPreferenceChanged() { + Camera.this.onSharedPreferenceChanged(); + } + } +} diff --git a/src/com/android/camera/CameraSettings.java b/src/com/android/camera/CameraSettings.java index 24ab449..4935d07 100644 --- a/src/com/android/camera/CameraSettings.java +++ b/src/com/android/camera/CameraSettings.java @@ -78,6 +78,7 @@ public class CameraSettings { private final Parameters mParameters; private final CameraInfo[] mCameraInfo; private final int mCameraId; + private int[] mCameraIdByIndex; public CameraSettings(Activity activity, Parameters parameters, int cameraId, CameraInfo[] cameraInfo) { @@ -245,35 +246,35 @@ public class CameraSettings { } private void buildCameraId( - PreferenceGroup group, IconListPreference cameraId) { + PreferenceGroup group, IconListPreference preference) { int numOfCameras = mCameraInfo.length; if (numOfCameras < 2) { - removePreference(group, cameraId.getKey()); + removePreference(group, preference.getKey()); return; } - CharSequence entries[] = new CharSequence[numOfCameras]; - CharSequence entryValues[] = new CharSequence[numOfCameras]; - int[] iconIds = new int[numOfCameras]; - int[] largeIconIds = new int[numOfCameras]; - for (int i = 0; i < numOfCameras; i++) { - entryValues[i] = Integer.toString(i); - if (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT) { - entries[i] = mContext.getString( - R.string.pref_camera_id_entry_front); - iconIds[i] = R.drawable.ic_menuselect_camera_facing_front; - largeIconIds[i] = R.drawable.ic_viewfinder_camera_facing_front; - } else { - entries[i] = mContext.getString( - R.string.pref_camera_id_entry_back); - iconIds[i] = R.drawable.ic_menuselect_camera_facing_back; - largeIconIds[i] = R.drawable.ic_viewfinder_camera_facing_back; + CharSequence[] entryValues = new CharSequence[2]; + mCameraIdByIndex = new int[2]; + for (int i = 0 ; i < mCameraInfo.length ; ++i) { + int index = + (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT) + ? CameraInfo.CAMERA_FACING_FRONT + : CameraInfo.CAMERA_FACING_BACK; + if (entryValues[index] == null) { + entryValues[index] = "" + i; + mCameraIdByIndex[index] = i; + if (entryValues[((index == 1) ? 0 : 1)] != null) break; } } - cameraId.setEntries(entries); - cameraId.setEntryValues(entryValues); - cameraId.setIconIds(iconIds); - cameraId.setLargeIconIds(largeIconIds); + preference.setEntryValues(entryValues); + } + + int getCameraIdByIndex(int facingIndex) { + if (facingIndex > CameraInfo.CAMERA_FACING_FRONT || facingIndex < 0) { + Log.e(TAG, "Unsupported camera facing index " + facingIndex); + return mCameraIdByIndex[CameraInfo.CAMERA_FACING_BACK]; + } + return mCameraIdByIndex[facingIndex]; } private static boolean removePreference(PreferenceGroup group, String key) { diff --git a/src/com/android/camera/PreviewFrameLayout.java b/src/com/android/camera/PreviewFrameLayout.java index ee12c39..4ceb58c 100644 --- a/src/com/android/camera/PreviewFrameLayout.java +++ b/src/com/android/camera/PreviewFrameLayout.java @@ -22,11 +22,13 @@ import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; +import android.widget.RadioGroup; +import android.widget.RelativeLayout; /** * A layout which handles the preview aspect ratio. */ -public class PreviewFrameLayout extends ViewGroup { +public class PreviewFrameLayout extends RelativeLayout { private static final int MIN_HORIZONTAL_MARGIN = 10; // 10dp /** A callback to be invoked when the preview frame's size changes. */ @@ -36,6 +38,7 @@ public class PreviewFrameLayout extends ViewGroup { private double mAspectRatio = 4.0 / 3.0; private View mFrame; + private RadioGroup mCameraPicker; private OnSizeChangedListener mSizeListener; private final DisplayMetrics mMetrics = new DisplayMetrics(); @@ -57,6 +60,7 @@ public class PreviewFrameLayout extends ViewGroup { throw new IllegalStateException( "must provide child with id as \"frame\""); } + mCameraPicker = (RadioGroup) findViewById(R.id.camera_picker); } public void setAspectRatio(double ratio) { @@ -98,6 +102,16 @@ public class PreviewFrameLayout extends ViewGroup { if (mSizeListener != null) { mSizeListener.onSizeChanged(); } + if (mCameraPicker != null) { + mCameraPicker.measure( + MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.AT_MOST), + MeasureSpec.makeMeasureSpec(b - t, MeasureSpec.AT_MOST)); + int width = mCameraPicker.getMeasuredWidth(); + int height = mCameraPicker.getMeasuredHeight(); + int ct = t + ((ViewGroup.MarginLayoutParams) + mCameraPicker.getLayoutParams()).topMargin; + int cl = (r - l - width) / 2; + mCameraPicker.layout(cl, ct, cl + width, ct + height); + } } } - diff --git a/src/com/android/camera/VideoCamera.java b/src/com/android/camera/VideoCamera.java index 7ff6bec..f383dd8 100644 --- a/src/com/android/camera/VideoCamera.java +++ b/src/com/android/camera/VideoCamera.java @@ -19,6 +19,7 @@ package com.android.camera; import com.android.camera.gallery.IImage; import com.android.camera.gallery.IImageList; import com.android.camera.ui.CamcorderHeadUpDisplay; +import com.android.camera.ui.CameraPicker; import com.android.camera.ui.ControlPanel; import com.android.camera.ui.GLRootView; import com.android.camera.ui.GLView; @@ -133,6 +134,7 @@ public class VideoCamera extends NoSearchActivity private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); private ComboPreferences mPreferences; + private PreferenceGroup mPreferenceGroup; private PreviewFrameLayout mPreviewFrameLayout; private SurfaceView mVideoPreview; @@ -143,6 +145,8 @@ public class VideoCamera extends NoSearchActivity private CamcorderHeadUpDisplay mHeadUpDisplay; private ControlPanel mControlPanel; private MenuItem mSwitchTimeLapseMenuItem; + // Front/back camera picker for xlarge layout. + private CameraPicker mCameraPicker; private boolean mIsVideoCaptureIntent; private boolean mQuickCapture; @@ -198,6 +202,8 @@ public class VideoCamera extends NoSearchActivity // multiple cameras support private int mNumberOfCameras; private int mCameraId; + private int mFrontCameraId; + private int mBackCameraId; private GestureDetector mPopupGestureDetector; @@ -395,6 +401,8 @@ public class VideoCamera extends NoSearchActivity // ignore } + loadCameraPreferences(); + // Initialize after startPreview becuase this need mParameters. initializeControlPanel(); // xlarge devices use control panel. Other devices use head-up display. @@ -403,6 +411,7 @@ public class VideoCamera extends NoSearchActivity mHeadUpDisplay.setListener(new MyHeadUpDisplayListener()); initializeHeadUpDisplay(); } + initializeCameraPicker(); } private void changeHeadUpDisplayState() { @@ -421,16 +430,36 @@ public class VideoCamera extends NoSearchActivity } } - private void initializeHeadUpDisplay() { - if (mHeadUpDisplay == null) return; + private void initializeCameraPicker() { + mCameraPicker = (CameraPicker) findViewById(R.id.camera_picker); + if (mCameraPicker != null) { + ListPreference pref = mPreferenceGroup.findPreference( + CameraSettings.KEY_CAMERA_ID); + if (pref != null) { + mCameraPicker.initialize(pref); + mCameraPicker.setListener(new MyCameraPickerListener()); + } + } + } + + private void loadCameraPreferences() { CameraSettings settings = new CameraSettings(this, mParameters, mCameraId, CameraHolder.instance().getCameraInfo()); - PreferenceGroup group = settings.getPreferenceGroup(R.xml.video_preferences); + mPreferenceGroup = settings.getPreferenceGroup(R.xml.video_preferences); + mFrontCameraId = + settings.getCameraIdByIndex(CameraInfo.CAMERA_FACING_FRONT); + mBackCameraId = + settings.getCameraIdByIndex(CameraInfo.CAMERA_FACING_BACK); + } + + private void initializeHeadUpDisplay() { + if (mHeadUpDisplay == null) return; if (mIsVideoCaptureIntent) { - group = filterPreferenceScreenByIntent(group); + mPreferenceGroup = filterPreferenceScreenByIntent(mPreferenceGroup); } - mHeadUpDisplay.initialize(this, group, mOrientationCompensation, mCaptureTimeLapse); + mHeadUpDisplay.initialize(this, mPreferenceGroup, + mOrientationCompensation, mCaptureTimeLapse); } private void attachHeadUpDisplay() { @@ -470,11 +499,7 @@ public class VideoCamera extends NoSearchActivity CameraSettings.KEY_CAMERA_ID}; mControlPanel = (ControlPanel) findViewById(R.id.control_panel); if (mControlPanel != null) { - CameraSettings settings = new CameraSettings(this, mParameters, - mCameraId, CameraHolder.instance().getCameraInfo()); - mControlPanel.initialize(this, - settings.getPreferenceGroup(R.xml.video_preferences), keys, - false); + mControlPanel.initialize(this, mPreferenceGroup, keys, false); mControlPanel.setListener(new MyControlPanelListener()); mPopupGestureDetector = new GestureDetector(this, new PopupGestureListener()); @@ -1248,7 +1273,10 @@ public class VideoCamera extends NoSearchActivity R.string.switch_camera_id) .setOnMenuItemClickListener(new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { - switchCameraId((mCameraId + 1) % mNumberOfCameras); + CameraSettings.writePreferredCameraId(mPreferences, + ((mCameraId == mFrontCameraId) + ? mBackCameraId : mFrontCameraId)); + onSharedPreferenceChanged(); return true; } }).setIcon(android.R.drawable.ic_menu_camera); @@ -1293,7 +1321,6 @@ public class VideoCamera extends NoSearchActivity private void switchCameraId(int cameraId) { if (mPausing) return; mCameraId = cameraId; - CameraSettings.writePreferredCameraId(mPreferences, cameraId); finishRecorderAndCloseCamera(); @@ -1833,6 +1860,12 @@ public class VideoCamera extends NoSearchActivity } } + private class MyCameraPickerListener implements CameraPicker.Listener { + public void onSharedPreferenceChanged() { + VideoCamera.this.onSharedPreferenceChanged(); + } + } + @Override public boolean dispatchTouchEvent(MotionEvent m) { // Check if the popup window should be dismissed first. diff --git a/src/com/android/camera/ui/CameraPicker.java b/src/com/android/camera/ui/CameraPicker.java new file mode 100644 index 0000000..e2a3441 --- /dev/null +++ b/src/com/android/camera/ui/CameraPicker.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2010 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.ui; + +import com.android.camera.ListPreference; +import com.android.camera.R; + +import android.content.Context; +import android.hardware.Camera.CameraInfo; +import android.util.AttributeSet; +import android.view.View; +import android.widget.RadioButton; +import android.widget.RadioGroup; + +/** + * A view for switching the front/back camera. + */ +public class CameraPicker extends RadioGroup implements View.OnClickListener { + private RadioButton mFrontCamera; + private RadioButton mBackCamera; + private Listener mListener; + private ListPreference mPreference; + private CharSequence[] mCameras; + private int mCameraIndex; + + public CameraPicker(Context context, AttributeSet attrs) { + super(context, attrs); + } + + static public interface Listener { + public void onSharedPreferenceChanged(); + } + + public void setListener(Listener listener) { + mListener = listener; + } + + public void initialize(ListPreference pref) { + mPreference = pref; + mCameras = pref.getEntryValues(); + if (mCameras == null) return; + String cameraId = pref.getValue(); + setVisibility(View.VISIBLE); + if (mCameras[CameraInfo.CAMERA_FACING_FRONT].equals(cameraId)) { + mFrontCamera.setChecked(true); + mCameraIndex = CameraInfo.CAMERA_FACING_FRONT; + } else { + mBackCamera.setChecked(true); + mCameraIndex = CameraInfo.CAMERA_FACING_BACK; + } + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + mFrontCamera = (RadioButton) findViewById(R.id.camera_switch_to_front); + mFrontCamera.setOnClickListener(this); + mBackCamera = (RadioButton) findViewById(R.id.camera_switch_to_back); + mBackCamera.setOnClickListener(this); + } + + public void onClick(View v) { + int newCameraIndex = (v == mFrontCamera) + ? CameraInfo.CAMERA_FACING_FRONT + : CameraInfo.CAMERA_FACING_BACK; + if (mCameraIndex != newCameraIndex) { + mCameraIndex = newCameraIndex; + mPreference.setValue((String) mCameras[mCameraIndex]); + mListener.onSharedPreferenceChanged(); + } + } + +} |