Last active
February 19, 2016 07:14
-
-
Save pfn/bba555a11a928cc67d20 to your computer and use it in GitHub Desktop.
colorizing passwords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait CharType | |
case object Uppercase extends CharType | |
case object Lowercase extends CharType | |
case object Symbol extends CharType | |
case object Digit extends CharType | |
case class Spans(chartype: CharType, start: Int, end: Int) | |
if (Option(strings.Get(PwDefs.PasswordField)) exists (_.Length > 0)) { | |
val passfield = new StandardFieldView(this) | |
passfield.first = first | |
first = false | |
passfield.hint = "Password" | |
passfield.text = Database.getField(entry, PwDefs.PasswordField).fold("": CharSequence) { p => | |
val (spans,last) = p.zipWithIndex.foldLeft((List.empty[Spans],Spans(Lowercase, 0, 0))) { case ((a,span), (c,i)) => | |
val chartype = if (c.isDigit) Digit | |
else if (c.isLower) Lowercase | |
else if (c.isUpper) Uppercase | |
else Symbol | |
if (span.chartype == chartype) { | |
(a,span) | |
} else { | |
(span.copy(end = i) :: a,Spans(chartype, i, i)) | |
} | |
} | |
val s = new SpannableString(p) | |
(last.copy(end = p.length) :: spans).foreach { span => | |
if (span.end > 0) { | |
val fg = span.chartype match { | |
case Uppercase => new ForegroundColorSpan(0xffff0000) | |
case Lowercase => new ForegroundColorSpan(0xff00ffff) | |
case Symbol => new ForegroundColorSpan(0xff0000ff) | |
case Digit => new ForegroundColorSpan(0xff00ff00) | |
} | |
s.setSpan(fg, span.start, span.end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE) | |
} | |
} | |
s | |
} | |
passfield.icon = R.drawable.ic_lock_outline_black_36dp | |
passfield.password = true | |
fieldlist.addView(passfield) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment