]> Pileus Git - ~andy/freeotp/blobdiff - src/org/fedorahosted/freeotp/MainActivity.java
Relicense to Apache 2.0
[~andy/freeotp] / src / org / fedorahosted / freeotp / MainActivity.java
index c54950c1220c0184eee24712412068e40218e859..ad62761fa7f1655e4cdb48dc1062f4cc0cb40e81 100644 (file)
@@ -4,20 +4,18 @@
  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
  *
  * 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 <http://www.gnu.org/licenses/>.
+ * 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.
  */
 
 /*
 
 package org.fedorahosted.freeotp;
 
-import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
 import java.util.List;
 
 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
 
-import android.net.Uri;
-import android.os.Bundle;
 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.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.net.Uri;
+import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
@@ -85,7 +83,7 @@ public class MainActivity extends ListActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
+        setContentView(R.layout.main);
         ta = new TokenAdapter(this);
         setListAdapter(ta);
     }
@@ -93,43 +91,65 @@ public class MainActivity extends ListActivity {
     @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) {
-                               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 false;
-                               }
-
-                               new AlertDialog.Builder(MainActivity.this)
-                                       .setTitle(R.string.install_title)
-                                       .setMessage(R.string.install_message)
-                                       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
-                                               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();
-                                                       }
+                               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();
                                                }
-                                       })
-                                       .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
-                                               public void onClick(DialogInterface dialogInterface, int i) {
+                                       }
+                               };
+
+                               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;
                                                }
-                                       })
-                                       .create().show();
+
+                                               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;
                        }
@@ -137,18 +157,16 @@ public class MainActivity extends ListActivity {
 
         return true;
     }
-    
-    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
-        if (resultCode == RESULT_OK) {
-            try {
+
+       @Override
+       public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+               if (resultCode == RESULT_OK) {
+                       try {
                                ta.add(this, intent.getStringExtra("SCAN_RESULT"));
-                       } catch (NoSuchAlgorithmException e) {
-                               Toast.makeText(this, R.string.token_scan_invalid, Toast.LENGTH_SHORT).show();
-                               e.printStackTrace();
                        } catch (TokenUriInvalidException e) {
-                               Toast.makeText(this, R.string.token_scan_invalid, Toast.LENGTH_SHORT).show();
+                               Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                        }
-        }
-    }
+               }
+       }
 }