4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 package org.fedorahosted.freeotp;
23 import java.util.List;
25 import android.annotation.TargetApi;
26 import android.app.AlertDialog;
27 import android.app.Dialog;
28 import android.content.DialogInterface;
29 import android.hardware.Camera;
30 import android.hardware.Camera.CameraInfo;
31 import android.hardware.Camera.Parameters;
32 import android.os.Bundle;
33 import android.os.Handler;
34 import android.os.Message;
35 import android.view.Surface;
36 import android.view.SurfaceHolder;
37 import android.view.SurfaceView;
38 import android.view.View;
39 import android.widget.TextView;
41 public class CameraDialogFragment extends BaseAlertDialogFragment
42 implements SurfaceHolder.Callback, Camera.AutoFocusCallback {
43 public static final String FRAGMENT_TAG = "fragment_camera";
45 private final CameraInfo mCameraInfo = new CameraInfo();
46 private final DecodeAsyncTask mDecodeAsyncTask;
47 private final int mCameraId;
48 private Handler mHandler;
49 private Camera mCamera;
51 public CameraDialogFragment() {
52 super(R.string.scan_qr_code, R.layout.camera,
53 android.R.string.cancel, R.string.manual_entry, 0);
55 // Find a back-facing camera, otherwise use the first camera.
57 for (cameraId = Camera.getNumberOfCameras() - 1; cameraId > 0; cameraId--) {
58 Camera.getCameraInfo(cameraId, mCameraInfo);
59 if (mCameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
64 // Create the decoder thread
65 mDecodeAsyncTask = new DecodeAsyncTask() {
67 protected void onPostExecute(String result) {
68 super.onPostExecute(result);
70 ((MainActivity) getActivity()).tokenURIReceived(result);
77 public void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
80 // If we have no cameras, open the manual dialog
82 new ManualDialogFragment().show(getFragmentManager(),
83 ManualDialogFragment.FRAGMENT_TAG);
88 mDecodeAsyncTask.execute();
92 public void onDestroy() {
94 mDecodeAsyncTask.cancel(true);
98 protected void onViewInflated(View view) {
99 SurfaceView sv = (SurfaceView) view.findViewById(R.id.camera_surfaceview);
100 sv.getHolder().addCallback(this);
104 public void onClick(DialogInterface dialog, int which) {
105 if (which != AlertDialog.BUTTON_NEUTRAL)
108 new ManualDialogFragment().show(getFragmentManager(),
109 ManualDialogFragment.FRAGMENT_TAG);
113 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
117 // The code in this section comes from the developer docs. See:
118 // http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)
121 switch (getActivity().getWindowManager().getDefaultDisplay().getRotation()) {
122 case Surface.ROTATION_0:
125 case Surface.ROTATION_90:
128 case Surface.ROTATION_180:
131 case Surface.ROTATION_270:
137 switch (mCameraInfo.facing) {
138 case Camera.CameraInfo.CAMERA_FACING_FRONT:
139 result = (mCameraInfo.orientation + rotation) % 360;
140 result = (360 - result) % 360; // compensate the mirror
143 case Camera.CameraInfo.CAMERA_FACING_BACK:
144 result = (mCameraInfo.orientation - rotation + 360) % 360;
148 mCamera.setDisplayOrientation(result);
149 mCamera.startPreview();
151 if (mHandler != null)
152 mHandler.sendEmptyMessageDelayed(0, 100);
157 public void surfaceCreated(SurfaceHolder holder) {
158 surfaceDestroyed(holder);
162 mCamera = Camera.open(mCameraId);
163 mCamera.setPreviewDisplay(holder);
164 mCamera.setPreviewCallback(mDecodeAsyncTask);
165 } catch (Exception e) {
167 surfaceDestroyed(holder);
169 // Show error message
170 Dialog d = getDialog();
171 SurfaceView sv = (SurfaceView) d.findViewById(R.id.camera_surfaceview);
172 TextView tv = (TextView) d.findViewById(R.id.camera_textview);
173 sv.setVisibility(View.INVISIBLE);
174 tv.setVisibility(View.VISIBLE);
178 // Set auto-focus mode
179 Parameters params = mCamera.getParameters();
180 List<String> modes = params.getSupportedFocusModes();
181 if (modes.contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
182 params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
183 else if (modes.contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
184 params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
185 else if (modes.contains(Parameters.FOCUS_MODE_AUTO)) {
186 params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
187 mHandler = new Handler() {
189 public void handleMessage(Message msg) {
190 super.handleMessage(msg);
191 mCamera.autoFocus(CameraDialogFragment.this);
195 mCamera.setParameters(params);
199 public void surfaceDestroyed(SurfaceHolder holder) {
203 if (mHandler != null) {
204 mCamera.cancelAutoFocus();
205 mHandler.removeMessages(0);
209 mCamera.stopPreview();
210 mCamera.setPreviewCallback(null);
216 public void onAutoFocus(boolean success, Camera camera) {
217 if (mHandler != null)
218 mHandler.sendEmptyMessageDelayed(0, 1000);