]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/MainActivity.java
Tablet UI rewrite
[~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 import org.fedorahosted.freeotp.adapters.TokenAdapter;
44
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;
62
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
70         });
71
72         private TokenAdapter ta;
73
74         private String findAppPackage(Intent i) {
75                 PackageManager pm = getPackageManager();
76                 List<ResolveInfo> ril = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
77                 if (ril != null) {
78                         for (ResolveInfo ri : ril) {
79                                 if (PROVIDERS.contains(ri.activityInfo.packageName))
80                                         return ri.activityInfo.packageName;
81                         }
82                 }
83
84                 return null;
85         }
86
87     @Override
88     protected void onCreate(Bundle savedInstanceState) {
89                 super.onCreate(savedInstanceState);
90                 setContentView(R.layout.main);
91
92                 ta = new TokenAdapter(this);
93                 ((GridView) findViewById(R.id.grid)).setAdapter(ta);
94
95                 DataSetObserver dso = new DataSetObserver() {
96                         @Override
97                         public void onChanged() {
98                                 super.onChanged();
99                                 if (ta.getCount() == 0)
100                                         findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
101                                 else
102                                         findViewById(android.R.id.empty).setVisibility(View.GONE);
103                         }
104                 };
105                 ta.registerDataSetObserver(dso);
106                 dso.onChanged();
107     }
108
109     @Override
110     public boolean onCreateOptionsMenu(Menu menu) {
111         getMenuInflater().inflate(R.menu.main, menu);
112
113         menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
114                         @Override
115                         public boolean onMenuItemClick(MenuItem item) {
116                                 AlertDialog ad = new AddTokenDialog(MainActivity.this) {
117                                         @Override
118                                         public void addToken(String uri) {
119                                                 try {
120                                                         ta.add(uri);
121                                                 } catch (TokenUriInvalidException e) {
122                                                         Toast.makeText(MainActivity.this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
123                                                         e.printStackTrace();
124                                                 }
125                                         }
126                                 };
127
128                                 ad.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.scan_qr_code), new OnClickListener() {
129                                         @Override
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);
137
138                                                 String pkg = findAppPackage(i);
139                                                 if (pkg != null) {
140                                                         i.setPackage(pkg);
141                                                         startActivityForResult(i, 0);
142                                                         return;
143                                                 }
144
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() {
149                                                                 @Override
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);
153                                                                         try {
154                                                                                 startActivity(intent);
155                                                                         } catch (ActivityNotFoundException e) {
156                                                                                 e.printStackTrace();
157                                                                         }
158                                                                 }
159                                                         })
160                                                         .setNegativeButton(R.string.no, new OnClickListener() {
161                                                                 @Override
162                                                                 public void onClick(DialogInterface dialogInterface, int i) {
163                                                                         return;
164                                                                 }
165                                                         })
166                                                         .create().show();
167                                         }
168                                 });
169
170                                 ad.show();
171
172                         return false;
173                         }
174                 });
175
176         return true;
177     }
178
179         @Override
180         public void onActivityResult(int requestCode, int resultCode, Intent intent) {
181                 if (resultCode == RESULT_OK) {
182                         try {
183                                 ta.add(intent.getStringExtra("SCAN_RESULT"));
184                         } catch (TokenUriInvalidException e) {
185                                 Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
186                                 e.printStackTrace();
187                         }
188                 }
189         }
190 }