]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/MainActivity.java
efcdae338836113c76752b86b90ac87474ceefb1
[~andy/freeotp] / src / org / fedorahosted / freeotp / MainActivity.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 /*
22  * Portions Copyright 2009 ZXing authors
23  *
24  * Licensed under the Apache License, Version 2.0 (the "License");
25  * you may not use this file except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *      http://www.apache.org/licenses/LICENSE-2.0
29  *
30  * Unless required by applicable law or agreed to in writing, software
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  */
36
37 package org.fedorahosted.freeotp;
38
39 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
40 import org.fedorahosted.freeotp.adapters.TokenAdapter;
41
42 import android.app.Activity;
43 import android.content.pm.PackageManager;
44 import android.database.DataSetObserver;
45 import android.hardware.Camera;
46 import android.hardware.Camera.CameraInfo;
47 import android.os.Bundle;
48 import android.view.Menu;
49 import android.view.MenuItem;
50 import android.view.MenuItem.OnMenuItemClickListener;
51 import android.view.View;
52 import android.widget.GridView;
53 import android.widget.Toast;
54
55 public class MainActivity extends Activity implements OnMenuItemClickListener {
56         private TokenAdapter mTokenAdapter;
57         private DataSetObserver mDataSetObserver;
58
59     @Override
60     protected void onCreate(Bundle savedInstanceState) {
61                 super.onCreate(savedInstanceState);
62                 setContentView(R.layout.main);
63
64                 mTokenAdapter = new TokenAdapter(this);
65                 ((GridView) findViewById(R.id.grid)).setAdapter(mTokenAdapter);
66
67                 mDataSetObserver = new DataSetObserver() {
68                         @Override
69                         public void onChanged() {
70                                 super.onChanged();
71                                 if (mTokenAdapter.getCount() == 0)
72                                         findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
73                                 else
74                                         findViewById(android.R.id.empty).setVisibility(View.GONE);
75                         }
76                 };
77                 mTokenAdapter.registerDataSetObserver(mDataSetObserver);
78                 mDataSetObserver.onChanged();
79     }
80
81         @Override
82         protected void onDestroy() {
83                 super.onDestroy();
84                 mTokenAdapter.unregisterDataSetObserver(mDataSetObserver);
85         }
86
87     @Override
88     public boolean onCreateOptionsMenu(Menu menu) {
89         getMenuInflater().inflate(R.menu.main, menu);
90         menu.findItem(R.id.action_add).setOnMenuItemClickListener(this);
91                 menu.findItem(R.id.action_about).setOnMenuItemClickListener(this);
92         return true;
93     }
94
95         @Override
96         public boolean onMenuItemClick(MenuItem item) {
97                 switch (item.getItemId()) {
98                 case R.id.action_add:
99                         // Look for a back-facing camera.
100                         boolean backCamera = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
101
102                         // If the above method doesn't return a camera, try to work around it.
103                         // Some buggy implementations return false above even when there is a camera.
104                         for (int i = Camera.getNumberOfCameras() - 1; i >= 0 && !backCamera; i--) {
105                                 CameraInfo ci = new CameraInfo();
106                                 Camera.getCameraInfo(i, ci);
107                                 backCamera = ci.facing == CameraInfo.CAMERA_FACING_BACK;
108                         }
109
110                         // If the device has a camera available, try to scan for QR code
111                         if (backCamera) {
112                                 new CameraDialogFragment().show(getFragmentManager(),
113                                                 CameraDialogFragment.FRAGMENT_TAG);
114                         } else {
115                                 new ManualDialogFragment().show(getFragmentManager(),
116                                                 ManualDialogFragment.FRAGMENT_TAG);
117                         }
118
119                         return true;
120
121                 case R.id.action_about:
122                         new AboutDialogFragment().show(getFragmentManager(),
123                                         AboutDialogFragment.FRAGMENT_TAG);
124                         return true;
125                 }
126
127                 return false;
128         }
129
130         public void tokenURIReceived(String uri) {
131                 try {
132                         mTokenAdapter.add(uri);
133                 } catch (TokenUriInvalidException e) {
134                         Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
135                         e.printStackTrace();
136                 }
137         }
138 }