1 /* 2 * Copyright (C) 2010 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.ui; 18 19 import com.android.camera.CameraPreference.OnPreferenceChangedListener; 20 import com.android.camera.ListPreference; 21 import com.android.camera.R; 22 23 import android.content.Context; 24 import android.hardware.Camera.CameraInfo; 25 import android.view.View; 26 27 /** 28 * A view for switching the front/back camera. 29 */ 30 public class CameraPicker extends RotateImageView implements View.OnClickListener { 31 private static int mImageResource; 32 33 private OnPreferenceChangedListener mListener; 34 private ListPreference mPreference; 35 private CharSequence[] mCameras; 36 private int mCameraFacing; 37 38 public CameraPicker(Context context) { 39 super(context); 40 setImageResource(mImageResource); 41 setContentDescription(getResources().getString( 42 R.string.accessibility_camera_picker)); 43 } 44 45 public static void setImageResourceId(int imageResource) { 46 mImageResource = imageResource; 47 } 48 49 public void setListener(OnPreferenceChangedListener listener) { 50 mListener = listener; 51 } 52 53 public void initialize(ListPreference pref) { 54 mPreference = pref; 55 mCameras = pref.getEntryValues(); 56 if (mCameras == null) return; 57 setOnClickListener(this); 58 String cameraId = pref.getValue(); 59 setVisibility(View.VISIBLE); 60 if (mCameras[CameraInfo.CAMERA_FACING_FRONT].equals(cameraId)) { 61 mCameraFacing = CameraInfo.CAMERA_FACING_FRONT; 62 } else { 63 mCameraFacing = CameraInfo.CAMERA_FACING_BACK; 64 } 65 } 66 67 @Override 68 public void onClick(View v) { 69 if (mCameras == null) return; 70 int newCameraIndex = (mCameraFacing == CameraInfo.CAMERA_FACING_BACK) 71 ? CameraInfo.CAMERA_FACING_FRONT 72 : CameraInfo.CAMERA_FACING_BACK; 73 mCameraFacing = newCameraIndex; 74 mPreference.setValue((String) mCameras[mCameraFacing]); 75 mListener.onSharedPreferenceChanged(); 76 } 77 } 78