]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/CameraDialogFragment.java
c3c23dd028cbcaad8c48c02d6bb2f85aae49d022
[~andy/freeotp] / src / org / fedorahosted / freeotp / CameraDialogFragment.java
1 /*
2  * FreeOTP
3  *
4  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
5  *
6  * Copyright (C) 2013  Nathaniel McCallum, Red Hat
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 package org.fedorahosted.freeotp;
22
23 import java.util.List;
24
25 import android.annotation.TargetApi;
26 import android.app.AlertDialog;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.hardware.Camera;
30 import android.hardware.Camera.Parameters;
31 import android.view.Surface;
32 import android.view.SurfaceHolder;
33 import android.view.SurfaceView;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.TextView;
37
38 public class CameraDialogFragment extends BaseAlertDialogFragment implements SurfaceHolder.Callback {
39         public static final String FRAGMENT_TAG = "fragment_camera";
40
41         private DecodeAsyncTask mDecodeAsyncTask;
42         private Camera mCamera;
43
44         public CameraDialogFragment() {
45                 super(R.string.scan_qr_code, R.layout.camera,
46                         android.R.string.cancel, R.string.manual_entry, 0);
47         }
48
49         @Override
50         protected void onViewInflated(View view) {
51                 SurfaceView sv = (SurfaceView) view.findViewById(R.id.camera_surfaceview);
52                 sv.getHolder().addCallback(this);
53         }
54
55         @Override
56         public void onClick(DialogInterface dialog, int which) {
57                 if (which == AlertDialog.BUTTON_NEUTRAL) {
58                         new ManualDialogFragment().show(getFragmentManager(),
59                                         ManualDialogFragment.FRAGMENT_TAG);
60                 }
61         }
62
63         @Override
64         public void onDestroyView() {
65                 if (mDecodeAsyncTask != null)
66                         mDecodeAsyncTask.cancel(true);
67
68                 super.onDestroyView();
69         }
70
71         @Override
72         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
73                 if (mCamera == null)
74                         return;
75
76                 WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
77                 switch (wm.getDefaultDisplay().getRotation()) {
78                 case Surface.ROTATION_0:
79                         mCamera.setDisplayOrientation(90);
80                         break;
81                 case Surface.ROTATION_270:
82                         mCamera.setDisplayOrientation(180);
83                         break;
84                 }
85
86                 mCamera.startPreview();
87         }
88
89         @Override
90         @TargetApi(14)
91         public void surfaceCreated(SurfaceHolder holder) {
92                 try {
93                         mCamera = Camera.open();
94                         mCamera.setPreviewDisplay(holder);
95
96                         // Create the decoder thread
97                         mDecodeAsyncTask = new DecodeAsyncTask() {
98                                 @Override
99                                 protected void onPostExecute(String result) {
100                                         super.onPostExecute(result);
101                                         if (result != null)
102                                                 ((MainActivity) getActivity()).tokenURIReceived(result);
103                                         mDecodeAsyncTask = null;
104                                         dismiss();
105                                 }
106                         };
107                         mDecodeAsyncTask.execute();
108                         mCamera.setPreviewCallback(mDecodeAsyncTask);
109
110                         // Set auto-focus mode
111                         Parameters params = mCamera.getParameters();
112                         List<String> modes = params.getSupportedFocusModes();
113                         if (modes.contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
114                                 params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
115                         else if (modes.contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
116                                 params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
117                         else if (modes.contains(Parameters.FOCUS_MODE_AUTO))
118                                 params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
119                         mCamera.setParameters(params);
120                 } catch (Exception e) {
121                         SurfaceView sv = (SurfaceView) getDialog().findViewById(R.id.camera_surfaceview);
122                         sv.setVisibility(View.INVISIBLE);
123                         TextView tv = (TextView) getDialog().findViewById(R.id.camera_textview);
124                         tv.setVisibility(View.VISIBLE);
125                         e.printStackTrace();
126                 }
127         }
128
129         @Override
130         public void surfaceDestroyed(SurfaceHolder holder) {
131                 if (mCamera == null)
132                         return;
133
134                 mCamera.stopPreview();
135                 mCamera.setPreviewCallback(null);
136                 mCamera.release();
137                 mCamera = null;
138         }
139 }