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;
43 import org.fedorahosted.freeotp.adapters.TokenAdapter;
45 import android.app.Activity;
46 import android.app.AlertDialog;
47 import android.content.ActivityNotFoundException;
48 import android.content.DialogInterface;
49 import android.content.DialogInterface.OnClickListener;
50 import android.content.Intent;
51 import android.content.pm.PackageManager;
52 import android.content.pm.ResolveInfo;
53 import android.database.DataSetObserver;
54 import android.net.Uri;
55 import android.os.Bundle;
56 import android.view.Menu;
57 import android.view.MenuItem;
58 import android.view.MenuItem.OnMenuItemClickListener;
59 import android.view.View;
60 import android.widget.GridView;
61 import android.widget.Toast;
63 public class MainActivity extends Activity {
64 private static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
65 private static final List<String> PROVIDERS = Arrays.asList(new String[] {
66 "com.google.zxing.client.android", // Barcode Scanner
67 "com.srowen.bs.android", // Barcode Scanner+
68 "com.srowen.bs.android.simple", // Barcode Scanner+ Simple
69 "com.google.android.apps.unveil" // Google Goggles
72 private TokenAdapter ta;
74 private String findAppPackage(Intent i) {
75 PackageManager pm = getPackageManager();
76 List<ResolveInfo> ril = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
78 for (ResolveInfo ri : ril) {
79 if (PROVIDERS.contains(ri.activityInfo.packageName))
80 return ri.activityInfo.packageName;
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 setContentView(R.layout.main);
92 ta = new TokenAdapter(this);
93 ((GridView) findViewById(R.id.grid)).setAdapter(ta);
95 DataSetObserver dso = new DataSetObserver() {
97 public void onChanged() {
99 if (ta.getCount() == 0)
100 findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
102 findViewById(android.R.id.empty).setVisibility(View.GONE);
105 ta.registerDataSetObserver(dso);
110 public boolean onCreateOptionsMenu(Menu menu) {
111 getMenuInflater().inflate(R.menu.main, menu);
113 menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
115 public boolean onMenuItemClick(MenuItem item) {
116 AlertDialog ad = new AddTokenDialog(MainActivity.this) {
118 public void addToken(String uri) {
121 } catch (TokenUriInvalidException e) {
122 Toast.makeText(MainActivity.this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
128 ad.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.scan_qr_code), new OnClickListener() {
130 public void onClick(DialogInterface dialog, int which) {
131 Intent i = new Intent(ACTION_SCAN);
132 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
133 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
134 i.addCategory(Intent.CATEGORY_DEFAULT);
135 i.putExtra("SCAN_MODE", "QR_CODE_MODE");
136 i.putExtra("SAVE_HISTORY", false);
138 String pkg = findAppPackage(i);
141 startActivityForResult(i, 0);
145 new AlertDialog.Builder(MainActivity.this)
146 .setTitle(R.string.install_title)
147 .setMessage(R.string.install_message)
148 .setPositiveButton(R.string.yes, new OnClickListener() {
150 public void onClick(DialogInterface dialogInterface, int i) {
151 Uri uri = Uri.parse("market://details?id=" + PROVIDERS.get(0));
152 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
154 startActivity(intent);
155 } catch (ActivityNotFoundException e) {
160 .setNegativeButton(R.string.no, new OnClickListener() {
162 public void onClick(DialogInterface dialogInterface, int i) {
176 menu.findItem(R.id.action_about).setOnMenuItemClickListener(new OnMenuItemClickListener() {
178 public boolean onMenuItemClick(MenuItem item) {
179 new AboutDialog(MainActivity.this).show();
188 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
189 if (resultCode == RESULT_OK) {
191 ta.add(intent.getStringExtra("SCAN_RESULT"));
192 } catch (TokenUriInvalidException e) {
193 Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();