]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/MainActivity.java
ad62761fa7f1655e4cdb48dc1062f4cc0cb40e81
[~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 java.util.Arrays;
40 import java.util.List;
41
42 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
43
44 import android.app.AlertDialog;
45 import android.app.ListActivity;
46 import android.content.ActivityNotFoundException;
47 import android.content.DialogInterface;
48 import android.content.DialogInterface.OnClickListener;
49 import android.content.Intent;
50 import android.content.pm.PackageManager;
51 import android.content.pm.ResolveInfo;
52 import android.net.Uri;
53 import android.os.Bundle;
54 import android.view.Menu;
55 import android.view.MenuItem;
56 import android.view.MenuItem.OnMenuItemClickListener;
57 import android.widget.Toast;
58
59 public class MainActivity extends ListActivity {
60         private static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
61         private static final List<String> PROVIDERS = Arrays.asList(new String[] {
62                 "com.google.zxing.client.android", // Barcode Scanner
63                 "com.srowen.bs.android",           // Barcode Scanner+
64                 "com.srowen.bs.android.simple",    // Barcode Scanner+ Simple
65                 "com.google.android.apps.unveil"   // Google Goggles
66         });
67
68         private TokenAdapter ta;
69
70         private String findAppPackage(Intent i) {
71                 PackageManager pm = getPackageManager();
72                 List<ResolveInfo> ril = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
73                 if (ril != null) {
74                         for (ResolveInfo ri : ril) {
75                                 if (PROVIDERS.contains(ri.activityInfo.packageName))
76                                         return ri.activityInfo.packageName;
77                         }
78                 }
79
80                 return null;
81         }
82
83     @Override
84     protected void onCreate(Bundle savedInstanceState) {
85         super.onCreate(savedInstanceState);
86         setContentView(R.layout.main);
87         ta = new TokenAdapter(this);
88         setListAdapter(ta);
89     }
90
91     @Override
92     public boolean onCreateOptionsMenu(Menu menu) {
93         getMenuInflater().inflate(R.menu.main, menu);
94
95         menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
96                         @Override
97                         public boolean onMenuItemClick(MenuItem item) {
98                                 AlertDialog ad = new AddTokenDialog(MainActivity.this) {
99                                         @Override
100                                         public void addToken(String uri) {
101                                                 try {
102                                                         ta.add(MainActivity.this, uri);
103                                                 } catch (TokenUriInvalidException e) {
104                                                         Toast.makeText(MainActivity.this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
105                                                         e.printStackTrace();
106                                                 }
107                                         }
108                                 };
109
110                                 ad.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.scan_qr_code), new OnClickListener() {
111                                         @Override
112                                         public void onClick(DialogInterface dialog, int which) {
113                                                 Intent i = new Intent(ACTION_SCAN);
114                                                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
115                                                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
116                                                 i.addCategory(Intent.CATEGORY_DEFAULT);
117                                                 i.putExtra("SCAN_MODE", "QR_CODE_MODE");
118                                                 i.putExtra("SAVE_HISTORY", false);
119
120                                                 String pkg = findAppPackage(i);
121                                                 if (pkg != null) {
122                                                         i.setPackage(pkg);
123                                                         startActivityForResult(i, 0);
124                                                         return;
125                                                 }
126
127                                                 new AlertDialog.Builder(MainActivity.this)
128                                                         .setTitle(R.string.install_title)
129                                                         .setMessage(R.string.install_message)
130                                                         .setPositiveButton(R.string.yes, new OnClickListener() {
131                                                                 @Override
132                                                                 public void onClick(DialogInterface dialogInterface, int i) {
133                                                                         Uri uri = Uri.parse("market://details?id=" + PROVIDERS.get(0));
134                                                                         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
135                                                                         try {
136                                                                                 startActivity(intent);
137                                                                         } catch (ActivityNotFoundException e) {
138                                                                                 e.printStackTrace();
139                                                                         }
140                                                                 }
141                                                         })
142                                                         .setNegativeButton(R.string.no, new OnClickListener() {
143                                                                 @Override
144                                                                 public void onClick(DialogInterface dialogInterface, int i) {
145                                                                         return;
146                                                                 }
147                                                         })
148                                                         .create().show();
149                                         }
150                                 });
151
152                                 ad.show();
153
154                         return false;
155                         }
156                 });
157
158         return true;
159     }
160
161         @Override
162         public void onActivityResult(int requestCode, int resultCode, Intent intent) {
163                 if (resultCode == RESULT_OK) {
164                         try {
165                                 ta.add(this, intent.getStringExtra("SCAN_RESULT"));
166                         } catch (TokenUriInvalidException e) {
167                                 Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
168                                 e.printStackTrace();
169                         }
170                 }
171         }
172 }