diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-15 17:33:00 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-15 17:33:00 -0500 |
commit | 7f864badb2ee0a1878459c9e533e10eb13dcc5ca (patch) | |
tree | e21c14359b0420c8953c10f59db9c5195aa2ff11 /app/src/main | |
parent | 2103a28f8f1c72d2829e935089fdda3950abd346 (diff) |
KeyInputFilter: Extract to its own class
It will be reused for entering public keys of peers.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/com/wireguard/android/ConfigEditFragment.java | 37 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/KeyInputFilter.java | 42 |
2 files changed, 43 insertions, 36 deletions
diff --git a/app/src/main/java/com/wireguard/android/ConfigEditFragment.java b/app/src/main/java/com/wireguard/android/ConfigEditFragment.java index a8e37155..488d7915 100644 --- a/app/src/main/java/com/wireguard/android/ConfigEditFragment.java +++ b/app/src/main/java/com/wireguard/android/ConfigEditFragment.java @@ -4,8 +4,6 @@ import android.content.Context; import android.os.Bundle; import android.text.InputFilter; import android.text.LoginFilter; -import android.text.SpannableStringBuilder; -import android.text.Spanned; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -17,7 +15,6 @@ import android.widget.EditText; import com.wireguard.android.databinding.ConfigEditFragmentBinding; import com.wireguard.config.Config; -import com.wireguard.crypto.KeyEncoding; /** * Fragment for editing a WireGuard configuration. @@ -58,39 +55,7 @@ public class ConfigEditFragment extends BaseConfigFragment { } }); final EditText privateKeyText = binding.getRoot().findViewById(R.id.private_key_text); - privateKeyText.setFilters(new InputFilter[]{ - new InputFilter() { - @Override - public CharSequence filter(final CharSequence source, - final int sStart, final int sEnd, - final Spanned dest, - final int dStart, final int dEnd) { - SpannableStringBuilder replacement = null; - int rIndex = 0; - final int dLength = dest.length(); - for (int sIndex = sStart; sIndex < sEnd; ++sIndex) { - final char c = source.charAt(sIndex); - final int dIndex = dStart + (sIndex - sStart); - // Restrict characters to the base64 character set. - // Ensure adding this character does not push the length over the limit. - if (((dIndex + 1 < KeyEncoding.KEY_LENGTH_BASE64 && isAllowed(c)) || - (dIndex + 1 == KeyEncoding.KEY_LENGTH_BASE64 && c == '=')) && - dLength + (sIndex - sStart) < KeyEncoding.KEY_LENGTH_BASE64) { - ++rIndex; - } else { - if (replacement == null) - replacement = new SpannableStringBuilder(source, sStart, sEnd); - replacement.delete(rIndex, rIndex + 1); - } - } - return replacement; - } - - private boolean isAllowed(final char c) { - return Character.isLetterOrDigit(c) || c == '+' || c == '/'; - } - } - }); + privateKeyText.setFilters(new InputFilter[]{new KeyInputFilter()}); binding.setConfig(localConfig); return binding.getRoot(); } diff --git a/app/src/main/java/com/wireguard/android/KeyInputFilter.java b/app/src/main/java/com/wireguard/android/KeyInputFilter.java new file mode 100644 index 00000000..758528c9 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/KeyInputFilter.java @@ -0,0 +1,42 @@ +package com.wireguard.android; + +import android.text.InputFilter; +import android.text.SpannableStringBuilder; +import android.text.Spanned; + +import com.wireguard.crypto.KeyEncoding; + +/** + * InputFilter for entering WireGuard private/public keys encoded with base64. + */ +class KeyInputFilter implements InputFilter { + @Override + public CharSequence filter(final CharSequence source, + final int sStart, final int sEnd, + final Spanned dest, + final int dStart, final int dEnd) { + SpannableStringBuilder replacement = null; + int rIndex = 0; + final int dLength = dest.length(); + for (int sIndex = sStart; sIndex < sEnd; ++sIndex) { + final char c = source.charAt(sIndex); + final int dIndex = dStart + (sIndex - sStart); + // Restrict characters to the base64 character set. + // Ensure adding this character does not push the length over the limit. + if (((dIndex + 1 < KeyEncoding.KEY_LENGTH_BASE64 && isAllowed(c)) || + (dIndex + 1 == KeyEncoding.KEY_LENGTH_BASE64 && c == '=')) && + dLength + (sIndex - sStart) < KeyEncoding.KEY_LENGTH_BASE64) { + ++rIndex; + } else { + if (replacement == null) + replacement = new SpannableStringBuilder(source, sStart, sEnd); + replacement.delete(rIndex, rIndex + 1); + } + } + return replacement; + } + + private boolean isAllowed(final char c) { + return Character.isLetterOrDigit(c) || c == '+' || c == '/'; + } +} |