4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * Portions Copyright 2009 ZXing authors
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
28 * http://www.apache.org/licenses/LICENSE-2.0
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.
37 package org.fedorahosted.freeotp;
39 import java.util.Arrays;
40 import java.util.List;
42 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
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;
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
68 private TokenAdapter ta;
70 private String findAppPackage(Intent i) {
71 PackageManager pm = getPackageManager();
72 List<ResolveInfo> ril = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
74 for (ResolveInfo ri : ril) {
75 if (PROVIDERS.contains(ri.activityInfo.packageName))
76 return ri.activityInfo.packageName;
84 protected void onCreate(Bundle savedInstanceState) {
85 super.onCreate(savedInstanceState);
86 setContentView(R.layout.main);
87 ta = new TokenAdapter(this);
92 public boolean onCreateOptionsMenu(Menu menu) {
93 getMenuInflater().inflate(R.menu.main, menu);
95 menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
97 public boolean onMenuItemClick(MenuItem item) {
98 AlertDialog ad = new AddTokenDialog(MainActivity.this) {
100 public void addToken(String uri) {
102 ta.add(MainActivity.this, uri);
103 } catch (TokenUriInvalidException e) {
104 Toast.makeText(MainActivity.this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
110 ad.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.scan_qr_code), new OnClickListener() {
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);
120 String pkg = findAppPackage(i);
123 startActivityForResult(i, 0);
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() {
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);
136 startActivity(intent);
137 } catch (ActivityNotFoundException e) {
142 .setNegativeButton(R.string.no, new OnClickListener() {
144 public void onClick(DialogInterface dialogInterface, int i) {
162 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
163 if (resultCode == RESULT_OK) {
165 ta.add(this, intent.getStringExtra("SCAN_RESULT"));
166 } catch (TokenUriInvalidException e) {
167 Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();