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 org.fedorahosted.freeotp.Token.TokenUriInvalidException;
40 import org.fedorahosted.freeotp.adapters.TokenAdapter;
42 import android.app.Activity;
43 import android.content.pm.PackageManager;
44 import android.database.DataSetObserver;
45 import android.os.Bundle;
46 import android.view.Menu;
47 import android.view.MenuItem;
48 import android.view.MenuItem.OnMenuItemClickListener;
49 import android.view.View;
50 import android.widget.GridView;
51 import android.widget.Toast;
53 public class MainActivity extends Activity implements OnMenuItemClickListener {
54 private TokenAdapter mTokenAdapter;
55 private DataSetObserver mDataSetObserver;
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.main);
62 mTokenAdapter = new TokenAdapter(this);
63 ((GridView) findViewById(R.id.grid)).setAdapter(mTokenAdapter);
65 mDataSetObserver = new DataSetObserver() {
67 public void onChanged() {
69 if (mTokenAdapter.getCount() == 0)
70 findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
72 findViewById(android.R.id.empty).setVisibility(View.GONE);
75 mTokenAdapter.registerDataSetObserver(mDataSetObserver);
76 mDataSetObserver.onChanged();
80 protected void onDestroy() {
82 mTokenAdapter.unregisterDataSetObserver(mDataSetObserver);
86 public boolean onCreateOptionsMenu(Menu menu) {
87 getMenuInflater().inflate(R.menu.main, menu);
88 menu.findItem(R.id.action_add).setOnMenuItemClickListener(this);
89 menu.findItem(R.id.action_about).setOnMenuItemClickListener(this);
94 public boolean onMenuItemClick(MenuItem item) {
95 switch (item.getItemId()) {
97 // If the device has a camera available, try to scan for QR code
98 PackageManager pm = getPackageManager();
99 if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
100 new CameraDialogFragment().show(getFragmentManager(),
101 CameraDialogFragment.FRAGMENT_TAG);
103 new ManualDialogFragment().show(getFragmentManager(),
104 ManualDialogFragment.FRAGMENT_TAG);
109 case R.id.action_about:
110 new AboutDialogFragment().show(getFragmentManager(),
111 AboutDialogFragment.FRAGMENT_TAG);
118 public void tokenURIReceived(String uri) {
120 mTokenAdapter.add(uri);
121 } catch (TokenUriInvalidException e) {
122 Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();