Change font by Droid Sans Mono Slashed and activate by default, add it in password generator #86

This commit is contained in:
J-Jamet committed 2018-04-19 11:09:55 +02:00
1 parent fc6453beba
commit a367aeaf12
10 files changed
+229 -163

No files matched your search

@@ -230,9 +230,11 @@ public class EntryEditActivity extends LockingHideActivity
* Add a new view to fill in the information of the customized field
*/
private void addNewCustomField() {
EntryEditCustomField ees = new EntryEditCustomField(EntryEditActivity.this);
ees.setData("", new ProtectedString(false, ""));
entryExtraFieldsContainer.addView(ees);
EntryEditCustomField entryEditCustomField = new EntryEditCustomField(EntryEditActivity.this);
entryEditCustomField.setData("", new ProtectedString(false, ""));
boolean visibilityFontActivated = PreferencesUtil.fieldFontIsInVisibility(this);
entryEditCustomField.setFontVisibility(visibilityFontActivated);
entryExtraFieldsContainer.addView(entryEditCustomField);
// Scroll bottom
scrollView.post(() -> scrollView.fullScroll(ScrollView.FOCUS_DOWN));
@@ -483,10 +485,10 @@ public class EntryEditActivity extends LockingHideActivity
boolean visibilityFontActivated = PreferencesUtil.fieldFontIsInVisibility(this);
if (visibilityFontActivated) {
Util.applyFontVisibilityTo(entryUserNameView);
Util.applyFontVisibilityTo(entryPasswordView);
Util.applyFontVisibilityTo(entryConfirmationPasswordView);
Util.applyFontVisibilityTo(entryCommentView);
Util.applyFontVisibilityTo(this, entryUserNameView);
Util.applyFontVisibilityTo(this, entryPasswordView);
Util.applyFontVisibilityTo(this, entryConfirmationPasswordView);
Util.applyFontVisibilityTo(this, entryCommentView);
}
if (mEntry.allowExtraFields()) {
@@ -36,6 +36,7 @@ import android.widget.Toast;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.password.PasswordGenerator;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.utils.Util;
import java.util.Set;
@@ -46,6 +47,7 @@ public class GeneratePasswordDialogFragment extends DialogFragment {
private GeneratePasswordListener mListener;
private View root;
private EditText lengthTextView;
private EditText passwordView;
private CompoundButton uppercaseBox;
private CompoundButton lowercaseBox;
@@ -75,6 +77,9 @@ public class GeneratePasswordDialogFragment extends DialogFragment {
LayoutInflater inflater = getActivity().getLayoutInflater();
root = inflater.inflate(R.layout.generate_password, null);
passwordView = root.findViewById(R.id.password);
Util.applyFontVisibilityTo(getContext(), passwordView);
lengthTextView = root.findViewById(R.id.length);
uppercaseBox = root.findViewById(R.id.cb_uppercase);
@@ -109,9 +114,8 @@ public class GeneratePasswordDialogFragment extends DialogFragment {
builder.setView(root)
.setPositiveButton(R.string.accept, (dialog, id) -> {
EditText password = root.findViewById(R.id.password);
Bundle bundle = new Bundle();
bundle.putString(KEY_PASSWORD_ID, password.getText().toString());
bundle.putString(KEY_PASSWORD_ID, passwordView.getText().toString());
mListener.acceptPassword(bundle);
dismiss();
@@ -1,88 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.utils;
import android.os.Parcel;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpannableReplacer {
private final CharSequence mSource;
private final CharSequence mReplacement;
private final Matcher mMatcher;
private int mAppendPosition;
private final boolean mIsSpannable;
public static CharSequence replace(CharSequence source, String regex,
CharSequence replacement) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
return new SpannableReplacer(source, matcher, replacement).doReplace();
}
private SpannableReplacer(CharSequence source, Matcher matcher,
CharSequence replacement) {
mSource = source;
mReplacement = replacement;
mMatcher = matcher;
mAppendPosition = 0;
mIsSpannable = replacement instanceof Spannable;
}
private CharSequence doReplace() {
SpannableStringBuilder buffer = new SpannableStringBuilder();
while (mMatcher.find()) {
appendReplacement(buffer);
}
return appendTail(buffer);
}
private void appendReplacement(SpannableStringBuilder buffer) {
buffer.append(mSource.subSequence(mAppendPosition, mMatcher.start()));
CharSequence replacement = mIsSpannable
? copyCharSequenceWithSpans(mReplacement)
: mReplacement;
buffer.append(replacement);
mAppendPosition = mMatcher.end();
}
public SpannableStringBuilder appendTail(SpannableStringBuilder buffer) {
buffer.append(mSource.subSequence(mAppendPosition, mSource.length()));
return buffer;
}
// This is a weird way of copying spans, but I don't know any better way.
private CharSequence copyCharSequenceWithSpans(CharSequence string) {
Parcel parcel = Parcel.obtain();
try {
TextUtils.writeToParcel(string, parcel, 0);
parcel.setDataPosition(0);
return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
} finally {
parcel.recycle();
}
}
}
@@ -24,11 +24,6 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.StrikethroughSpan;
import android.widget.EditText;
import android.widget.TextView;
@@ -61,66 +56,18 @@ public class Util {
}
}
private final static String stringToStrikeThrough = "0";
/**
* Replace font by monospace and strike through all zeros, must be called after seText()
* Replace font by monospace, must be called after seText()
*/
public static void applyFontVisibilityTo(final TextView textView) {
textView.setText(strikeThroughToZero(textView.getText()));
textView.setTypeface(Typeface.MONOSPACE);
public static void applyFontVisibilityTo(final Context context, final TextView textView) {
Typeface typeFace=Typeface.createFromAsset(context.getAssets(),"fonts/DroidSansMonoSlashed.ttf");
textView.setTypeface(typeFace);
}
/**
* Replace font by monospace and strike through all zeros, must be called after seText()
* Replace font by monospace, must be called after seText()
*/
public static void applyFontVisibilityTo(final EditText editText) {
// Assign spans to default text
editText.setText(strikeThroughToZero(editText.getText()));
// Add spans for each new 0 character
class TextWatcherCustomFont implements TextWatcher {
private boolean applySpannable;
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
applySpannable = count < after;
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (applySpannable) {
String text = charSequence.toString();
if (text.contains(stringToStrikeThrough)) {
for (int index = text.indexOf(stringToStrikeThrough);
index >= 0; index = text.indexOf(stringToStrikeThrough,
index + 1)) {
editText.getText().setSpan(new StrikethroughSpan(),
index,
index + stringToStrikeThrough.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
@Override
public void afterTextChanged(Editable editable) {}
};
TextWatcher textWatcher = new TextWatcherCustomFont();
editText.addTextChangedListener(textWatcher);
editText.setTypeface(Typeface.MONOSPACE);
public static void applyFontVisibilityTo(final Context context, final EditText editText) {
applyFontVisibilityTo(context, (TextView) editText);
}
private static CharSequence strikeThroughToZero(final CharSequence text) {
if (text.toString().contains(stringToStrikeThrough)) {
SpannableString spannable = new SpannableString(stringToStrikeThrough);
spannable.setSpan(new StrikethroughSpan(),
0,
stringToStrikeThrough.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return SpannableReplacer.replace(text, stringToStrikeThrough, spannable);
}
return text;
}
}
@@ -115,7 +115,7 @@ public class EntryContentsView extends LinearLayout {
userNameContainerView.setVisibility(VISIBLE);
userNameView.setText(userName);
if (fontInVisibility)
Util.applyFontVisibilityTo(userNameView);
Util.applyFontVisibilityTo(getContext(), userNameView);
} else {
userNameContainerView.setVisibility(GONE);
}
@@ -134,7 +134,7 @@ public class EntryContentsView extends LinearLayout {
passwordContainerView.setVisibility(VISIBLE);
passwordView.setText(password);
if (fontInVisibility)
Util.applyFontVisibilityTo(passwordView);
Util.applyFontVisibilityTo(getContext(), passwordView);
passwordActionView.setVisibility(GONE);
} else {
passwordContainerView.setVisibility(GONE);
@@ -187,7 +187,7 @@ public class EntryContentsView extends LinearLayout {
commentContainerView.setVisibility(VISIBLE);
commentView.setText(comment);
if (fontInVisibility)
Util.applyFontVisibilityTo(commentView);
Util.applyFontVisibilityTo(getContext(), commentView);
} else {
commentContainerView.setVisibility(GONE);
}
@@ -66,7 +66,7 @@ public class EntryCustomField extends LinearLayout {
public void applyFontVisibility(boolean fontInVisibility) {
if (fontInVisibility)
Util.applyFontVisibilityTo(valueView);
Util.applyFontVisibilityTo(getContext(), valueView);
}
public void setLabel(String label) {
@@ -86,7 +86,7 @@ public class EntryEditCustomField extends RelativeLayout {
public void setFontVisibility(boolean applyFontVisibility) {
if (applyFontVisibility)
Util.applyFontVisibilityTo(valueView);
Util.applyFontVisibilityTo(getContext(), valueView);
}
public void deleteViewFromParent() {