]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/CameraDialogFragment.java
5fe64480836405428d0b5a499182c0f31d5098fc
[~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 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;
34
35 public class CameraDialogFragment extends BaseAlertDialogFragment implements SurfaceHolder.Callback {
36         public static final String FRAGMENT_TAG = "fragment_camera";
37
38         private DecodeAsyncTask mDecodeAsyncTask;
39         private Camera mCamera;
40
41         public CameraDialogFragment() {
42                 super(R.string.scan_qr_code, R.layout.camera,
43                         android.R.string.cancel, R.string.manual_entry, 0);
44         }
45
46         @Override
47         protected void onViewInflated(View view) {
48                 SurfaceView sv = (SurfaceView) view.findViewById(R.id.camera_surfaceview);
49                 sv.getHolder().addCallback(this);
50         }
51
52         @Override
53         public void onClick(DialogInterface dialog, int which) {
54                 if (which == AlertDialog.BUTTON_NEUTRAL) {
55                         new AddTokenDialog(getActivity()) {
56                                 @Override
57                                 public void addToken(String uri) {
58                                         ((MainActivity) getActivity()).tokenURIReceived(uri);
59                                 }
60                         }.show();
61                 }
62         }
63
64         @Override
65         public void onDestroyView() {
66                 if (mDecodeAsyncTask != null)
67                         mDecodeAsyncTask.cancel(true);
68
69                 super.onDestroyView();
70         }
71
72         @Override
73         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
74                 if (mCamera == null)
75                         return;
76
77                 WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
78                 switch (wm.getDefaultDisplay().getRotation()) {
79                 case Surface.ROTATION_0:
80                         mCamera.setDisplayOrientation(90);
81                         break;
82                 case Surface.ROTATION_270:
83                         mCamera.setDisplayOrientation(180);
84                         break;
85                 }
86
87                 mCamera.startPreview();
88         }
89
90         @Override
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                         params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
113                         mCamera.setParameters(params);
114                 } catch (Exception e) {
115                         SurfaceView sv = (SurfaceView) getDialog().findViewById(R.id.camera_surfaceview);
116                         sv.setVisibility(View.INVISIBLE);
117                         TextView tv = (TextView) getDialog().findViewById(R.id.camera_textview);
118                         tv.setVisibility(View.VISIBLE);
119                         e.printStackTrace();
120                 }
121         }
122
123         @Override
124         public void surfaceDestroyed(SurfaceHolder holder) {
125                 if (mCamera == null)
126                         return;
127
128                 mCamera.stopPreview();
129                 mCamera.setPreviewCallback(null);
130                 mCamera.release();
131                 mCamera = null;
132         }
133 }