]> Pileus Git - ~andy/freeotp/commitdiff
Add detailed invalid URI messages master
authorAndy Spencer <andy753421@gmail.com>
Wed, 5 Feb 2014 20:32:25 +0000 (20:32 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 5 Feb 2014 22:40:27 +0000 (22:40 +0000)
res/values/strings.xml
src/org/fedorahosted/freeotp/MainActivity.java
src/org/fedorahosted/freeotp/Token.java

index 3fda9b6f0b71e6e3af2d6e187e277f6bddb877f8..fb2a2883d1d5bfdbd694233a56f23ef738049798 100644 (file)
     <string name="link_apache2">&lt;a href=&quot;http://www.apache.org/licenses/LICENSE-2.0.html&quot;&gt;Apache 2.0&lt;/a&gt;</string>
 
     <string name="error_camera_open">Error while opening camera!</string>
+    <string name="error_no_scheme">No scheme provided</string>
+    <string name="error_no_authority">No authority provided</string>
+    <string name="error_no_path">No path provided</string>
+    <string name="error_no_algorithm">No algorithm provided</string>
+    <string name="error_invalid_scheme">Invalid scheme</string>
+    <string name="error_invalid_authority">Invalid authority</string>
+    <string name="error_invalid_path">Invalid path</string>
+    <string name="error_invalid_digits">Invalid digits</string>
+    <string name="error_invalid_number">Invalid number</string>
+    <string name="error_invalid_counter">Invalid counter</string>
+    <string name="error_invalid_period">Invalid period</string>
+    <string name="error_invalid_secret">Invalid secret</string>
 
     <plurals name="tokens_selected">
         <item quantity="one">%d token selected</item>
index 0a65fc4e8cb040b3de43afa60156004888127757..be77fb0db7a5dbba724741e09c12b6119474091c 100644 (file)
@@ -110,7 +110,10 @@ public class MainActivity extends Activity implements OnMenuItemClickListener {
                try {
                        mTokenAdapter.add(uri);
                } catch (TokenUriInvalidException e) {
-                       Toast.makeText(this, R.string.invalid_token, Toast.LENGTH_SHORT).show();
+                       String text = getString(R.string.invalid_token);
+                       if (e.getErrorResourceID() != 0)
+                               text = text + "\n" + getString(e.getErrorResourceID());
+                       Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                }
        }
index 43342b1b566f46303282e3cb6376738446b60615..0e111e2264eb07bcf9ecb6c5e3d0c3c7a1cc209f 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Locale;
 import javax.crypto.Mac;
 import javax.crypto.spec.SecretKeySpec;
 
+import android.content.res.Resources;
 import android.net.Uri;
 
 import com.google.android.apps.authenticator.Base32String;
@@ -36,6 +37,13 @@ import com.google.android.apps.authenticator.Base32String.DecodingException;
 public class Token {
        public static class TokenUriInvalidException extends Exception {
                private static final long serialVersionUID = -1108624734612362345L;
+               private static int errorResourceID = 0;
+               public TokenUriInvalidException(int id) {
+                       this.errorResourceID = id;
+               }
+               public int getErrorResourceID() {
+                       return this.errorResourceID;
+               }
        }
 
        public static enum TokenType {
@@ -58,24 +66,28 @@ public class Token {
                String authority = uri.getAuthority();
                String path = uri.getPath();
 
-               if (scheme == null || authority == null || path == null)
-                       throw new TokenUriInvalidException();
+               if (scheme == null)
+                       throw new TokenUriInvalidException(R.string.error_no_scheme);
+               if (authority == null)
+                       throw new TokenUriInvalidException(R.string.error_no_authority);
+               if (path == null)
+                       throw new TokenUriInvalidException(R.string.error_no_path);
 
                if (!scheme.equals("otpauth"))
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_invalid_scheme);
 
                if (authority.equals("totp"))
                        mType = TokenType.TOTP;
                else if (authority.equals("hotp"))
                        mType = TokenType.HOTP;
                else
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_invalid_authority);
 
                // Strip the path of its leading '/'
                for (int i = 0; path.charAt(i) == '/'; i++)
                        path = path.substring(1);
                if (path.length() == 0)
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_invalid_path);
 
                int i = path.indexOf(':');
                mIssuerExt = i < 0 ? "" : path.substring(0, i);
@@ -89,7 +101,7 @@ public class Token {
                try {
                        Mac.getInstance("Hmac" + mAlgorithm);
                } catch (NoSuchAlgorithmException e1) {
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_no_algorithm);
                }
 
                try {
@@ -98,9 +110,9 @@ public class Token {
                                d = "6";
                        mDigits = Integer.parseInt(d);
                        if (mDigits != 6 && mDigits != 8)
-                               throw new TokenUriInvalidException();
+                               throw new TokenUriInvalidException(R.string.error_invalid_digits);
                } catch (NumberFormatException e) {
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_invalid_number);
                }
 
                switch (mType) {
@@ -111,7 +123,7 @@ public class Token {
                                        c = "0";
                                mCounter = Long.parseLong(c) - 1;
                        } catch (NumberFormatException e) {
-                               throw new TokenUriInvalidException();
+                               throw new TokenUriInvalidException(R.string.error_invalid_counter);
                        }
                        break;
                case TOTP:
@@ -121,7 +133,7 @@ public class Token {
                                        p = "30";
                                mPeriod = Integer.parseInt(p);
                        } catch (NumberFormatException e) {
-                               throw new TokenUriInvalidException();
+                               throw new TokenUriInvalidException(R.string.error_invalid_period);
                        }
                        break;
                }
@@ -130,7 +142,7 @@ public class Token {
                        String s = uri.getQueryParameter("secret");
                        mSecret = Base32String.decode(s);
                } catch (DecodingException e) {
-                       throw new TokenUriInvalidException();
+                       throw new TokenUriInvalidException(R.string.error_invalid_secret);
                }
        }