CameraPicker.java revision 59ddc8c1add2fcffe523e3877baad1b6cc5ba2fb
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
17package com.android.camera.ui;
18
19import com.android.camera.ListPreference;
20import com.android.camera.R;
21
22import android.content.Context;
23import android.hardware.Camera.CameraInfo;
24import android.util.AttributeSet;
25import android.view.View;
26import android.widget.RadioButton;
27import android.widget.RadioGroup;
28
29/**
30 * A view for switching the front/back camera.
31 */
32public class CameraPicker extends RadioGroup implements View.OnClickListener {
33    private RadioButton mFrontCamera;
34    private RadioButton mBackCamera;
35    private Listener mListener;
36    private ListPreference mPreference;
37    private CharSequence[] mCameras;
38    private int mCameraIndex;
39
40    public CameraPicker(Context context, AttributeSet attrs) {
41        super(context, attrs);
42    }
43
44    static public interface Listener {
45        public void onSharedPreferenceChanged();
46    }
47
48    public void setListener(Listener listener) {
49        mListener = listener;
50    }
51
52    public void initialize(ListPreference pref) {
53        mPreference = pref;
54        mCameras = pref.getEntryValues();
55        if (mCameras == null) return;
56        String cameraId = pref.getValue();
57        setVisibility(View.VISIBLE);
58        if (mCameras[CameraInfo.CAMERA_FACING_FRONT].equals(cameraId)) {
59            mFrontCamera.setChecked(true);
60            mCameraIndex = CameraInfo.CAMERA_FACING_FRONT;
61        } else {
62            mBackCamera.setChecked(true);
63            mCameraIndex = CameraInfo.CAMERA_FACING_BACK;
64        }
65    }
66
67    @Override
68    protected void onFinishInflate() {
69        super.onFinishInflate();
70        mFrontCamera = (RadioButton) findViewById(R.id.camera_switch_to_front);
71        mFrontCamera.setOnClickListener(this);
72        mBackCamera = (RadioButton) findViewById(R.id.camera_switch_to_back);
73        mBackCamera.setOnClickListener(this);
74    }
75
76    public void onClick(View v) {
77        int newCameraIndex = (v == mFrontCamera)
78                ? CameraInfo.CAMERA_FACING_FRONT
79                : CameraInfo.CAMERA_FACING_BACK;
80        if (mCameraIndex != newCameraIndex) {
81            mCameraIndex = newCameraIndex;
82            mPreference.setValue((String) mCameras[mCameraIndex]);
83            mListener.onSharedPreferenceChanged();
84        }
85    }
86
87}
88