X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Forg%2Ffedorahosted%2Ffreeotp%2FMainActivity.java;h=f72386da07b2064f4c5fcd985680787e342a56a3;hb=96a1198638125e8fff684f1e2df743ccf91fd0e8;hp=9a31a94592d7353f6408a464bed48368b5ee5f43;hpb=d40daba0d379b30d5951972ed34dc71d347127d7;p=~andy%2Ffreeotp diff --git a/src/org/fedorahosted/freeotp/MainActivity.java b/src/org/fedorahosted/freeotp/MainActivity.java index 9a31a94..f72386d 100644 --- a/src/org/fedorahosted/freeotp/MainActivity.java +++ b/src/org/fedorahosted/freeotp/MainActivity.java @@ -4,20 +4,18 @@ * Authors: Nathaniel McCallum * * Copyright (C) 2013 Nathaniel McCallum, Red Hat - * see file 'COPYING' for use and warranty information * - * This program is free software you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * http://www.apache.org/licenses/LICENSE-2.0 * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* @@ -38,137 +36,91 @@ package org.fedorahosted.freeotp; -import java.util.Arrays; -import java.util.List; - import org.fedorahosted.freeotp.Token.TokenUriInvalidException; +import org.fedorahosted.freeotp.adapters.TokenAdapter; -import android.app.AlertDialog; -import android.app.ListActivity; -import android.content.ActivityNotFoundException; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; +import android.app.Activity; import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.net.Uri; +import android.database.DataSetObserver; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; +import android.view.View; +import android.widget.GridView; import android.widget.Toast; -public class MainActivity extends ListActivity { - private static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN"; - private static final List PROVIDERS = Arrays.asList(new String[] { - "com.google.zxing.client.android", // Barcode Scanner - "com.srowen.bs.android", // Barcode Scanner+ - "com.srowen.bs.android.simple", // Barcode Scanner+ Simple - "com.google.android.apps.unveil" // Google Goggles - }); +public class MainActivity extends Activity implements OnMenuItemClickListener { + private TokenAdapter mTokenAdapter; + private DataSetObserver mDataSetObserver; - private TokenAdapter ta; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + mTokenAdapter = new TokenAdapter(this); + ((GridView) findViewById(R.id.grid)).setAdapter(mTokenAdapter); - private String findAppPackage(Intent i) { - PackageManager pm = getPackageManager(); - List ril = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY); - if (ril != null) { - for (ResolveInfo ri : ril) { - if (PROVIDERS.contains(ri.activityInfo.packageName)) - return ri.activityInfo.packageName; + mDataSetObserver = new DataSetObserver() { + @Override + public void onChanged() { + super.onChanged(); + if (mTokenAdapter.getCount() == 0) + findViewById(android.R.id.empty).setVisibility(View.VISIBLE); + else + findViewById(android.R.id.empty).setVisibility(View.GONE); } - } + }; + mTokenAdapter.registerDataSetObserver(mDataSetObserver); + mDataSetObserver.onChanged(); + } - return null; + @Override + protected void onDestroy() { + super.onDestroy(); + mTokenAdapter.unregisterDataSetObserver(mDataSetObserver); } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - ta = new TokenAdapter(this); - setListAdapter(ta); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); - - menu.findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - AlertDialog ad = new AddTokenDialog(MainActivity.this) { - @Override - public void addToken(String uri) { - try { - ta.add(MainActivity.this, uri); - } catch (TokenUriInvalidException e) { - Toast.makeText(MainActivity.this, R.string.invalid_token, Toast.LENGTH_SHORT).show(); - e.printStackTrace(); - } - } - }; - - ad.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.scan_qr_code), new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Intent i = new Intent(ACTION_SCAN); - i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); - i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - i.addCategory(Intent.CATEGORY_DEFAULT); - i.putExtra("SCAN_MODE", "QR_CODE_MODE"); - i.putExtra("SAVE_HISTORY", false); - - String pkg = findAppPackage(i); - if (pkg != null) { - i.setPackage(pkg); - startActivityForResult(i, 0); - return; - } - - new AlertDialog.Builder(MainActivity.this) - .setTitle(R.string.install_title) - .setMessage(R.string.install_message) - .setPositiveButton(R.string.yes, new OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Uri uri = Uri.parse("market://details?id=" + PROVIDERS.get(0)); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - try { - startActivity(intent); - } catch (ActivityNotFoundException e) { - e.printStackTrace(); - } - } - }) - .setNegativeButton(R.string.no, new OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - return; - } - }) - .create().show(); - } - }); - - ad.show(); - - return false; - } - }); - + menu.findItem(R.id.action_add).setOnMenuItemClickListener(this); + menu.findItem(R.id.action_about).setOnMenuItemClickListener(this); return true; } @Override - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - if (resultCode == RESULT_OK) { - try { - ta.add(this, intent.getStringExtra("SCAN_RESULT")); - } catch (TokenUriInvalidException e) { - Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show(); - e.printStackTrace(); + public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_add: + // If the device has a camera available, try to scan for QR code + PackageManager pm = getPackageManager(); + if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { + new CameraDialogFragment().show(getFragmentManager(), + CameraDialogFragment.FRAGMENT_TAG); + } else { + new ManualDialogFragment().show(getFragmentManager(), + ManualDialogFragment.FRAGMENT_TAG); } + + return true; + + case R.id.action_about: + new AboutDialogFragment().show(getFragmentManager(), + AboutDialogFragment.FRAGMENT_TAG); + return true; + } + + return false; + } + + public void tokenURIReceived(String uri) { + try { + mTokenAdapter.add(uri); + } catch (TokenUriInvalidException e) { + Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show(); + e.printStackTrace(); } } }