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 android.app.AlertDialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.hardware.Camera;
27 import android.hardware.Camera.Parameters;
28 import android.view.Surface;
29 import android.view.SurfaceHolder;
30 import android.view.SurfaceView;
31 import android.view.View;
32 import android.view.WindowManager;
33 import android.widget.TextView;
35 public class CameraDialogFragment extends BaseAlertDialogFragment implements SurfaceHolder.Callback {
36 public static final String FRAGMENT_TAG = "fragment_camera";
38 private DecodeAsyncTask mDecodeAsyncTask;
39 private Camera mCamera;
41 public CameraDialogFragment() {
42 super(R.string.scan_qr_code, R.layout.camera,
43 android.R.string.cancel, R.string.manual_entry, 0);
47 protected void onViewInflated(View view) {
48 SurfaceView sv = (SurfaceView) view.findViewById(R.id.camera_surfaceview);
49 sv.getHolder().addCallback(this);
53 public void onClick(DialogInterface dialog, int which) {
54 if (which == AlertDialog.BUTTON_NEUTRAL) {
55 new ManualDialogFragment().show(getFragmentManager(),
56 ManualDialogFragment.FRAGMENT_TAG);
61 public void onDestroyView() {
62 if (mDecodeAsyncTask != null)
63 mDecodeAsyncTask.cancel(true);
65 super.onDestroyView();
69 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
73 WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
74 switch (wm.getDefaultDisplay().getRotation()) {
75 case Surface.ROTATION_0:
76 mCamera.setDisplayOrientation(90);
78 case Surface.ROTATION_270:
79 mCamera.setDisplayOrientation(180);
83 mCamera.startPreview();
87 public void surfaceCreated(SurfaceHolder holder) {
89 mCamera = Camera.open();
90 mCamera.setPreviewDisplay(holder);
92 // Create the decoder thread
93 mDecodeAsyncTask = new DecodeAsyncTask() {
95 protected void onPostExecute(String result) {
96 super.onPostExecute(result);
98 ((MainActivity) getActivity()).tokenURIReceived(result);
99 mDecodeAsyncTask = null;
103 mDecodeAsyncTask.execute();
104 mCamera.setPreviewCallback(mDecodeAsyncTask);
106 // Set auto-focus mode
107 Parameters params = mCamera.getParameters();
108 params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
109 mCamera.setParameters(params);
110 } catch (Exception e) {
111 SurfaceView sv = (SurfaceView) getDialog().findViewById(R.id.camera_surfaceview);
112 sv.setVisibility(View.INVISIBLE);
113 TextView tv = (TextView) getDialog().findViewById(R.id.camera_textview);
114 tv.setVisibility(View.VISIBLE);
120 public void surfaceDestroyed(SurfaceHolder holder) {
124 mCamera.stopPreview();
125 mCamera.setPreviewCallback(null);