]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/MainActivity.java
Initial commit
[~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  * see file 'COPYING' for use and warranty information
8  *
9  * This program is free software you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 package org.fedorahosted.freeotp;
24
25 import java.security.NoSuchAlgorithmException;
26
27 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
28
29 import android.os.Bundle;
30 import android.app.ListActivity;
31 import android.content.Intent;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.MenuItem.OnMenuItemClickListener;
35 import android.widget.Toast;
36
37 public class MainActivity extends ListActivity {
38         private TokenAdapter ta;
39
40     @Override
41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         
44         ta = new TokenAdapter(this);
45         setListAdapter(ta);
46     }
47
48     @Override
49     public boolean onCreateOptionsMenu(Menu menu) {
50         getMenuInflater().inflate(R.menu.main, menu);
51         
52         menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
53                         public boolean onMenuItemClick(MenuItem item) {
54                                 Intent i = new Intent("com.google.zxing.client.android.SCAN");
55                         i.putExtra("SCAN_MODE", "QR_CODE_MODE");
56                         i.putExtra("SAVE_HISTORY", false);
57                         startActivityForResult(i, 0);
58                                 return false;
59                         }
60                 });
61         
62         return true;
63     }
64     
65     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
66         if (resultCode == RESULT_OK) {
67             try {
68                                 ta.add(this, intent.getStringExtra("SCAN_RESULT"));
69                         } catch (NoSuchAlgorithmException e) {
70                                 Toast.makeText(this, R.string.token_scan_invalid, Toast.LENGTH_SHORT).show();
71                                 e.printStackTrace();
72                         } catch (TokenUriInvalidException e) {
73                                 Toast.makeText(this, R.string.token_scan_invalid, Toast.LENGTH_SHORT).show();
74                                 e.printStackTrace();
75                         }
76         }
77     }
78 }