From be0893c7380abf90b87a7421b2db873312c003de Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 20 Jun 2015 11:04:52 -0400 Subject: [PATCH] Remove JDK 7 API and AWT from algorithm (for Android < 4.4) --- .../lyndir/masterpassword/MPIdenticon.java | 63 +++++---------- .../com/lyndir/masterpassword/gui/Res.java | 76 ++++++++++++++++++- .../masterpassword/gui/UnlockFrame.java | 2 +- 3 files changed, 92 insertions(+), 49 deletions(-) diff --git a/MasterPassword/Java/masterpassword-algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java b/MasterPassword/Java/masterpassword-algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java index b1b799e1..41894b67 100644 --- a/MasterPassword/Java/masterpassword-algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java +++ b/MasterPassword/Java/masterpassword-algorithm/src/main/java/com/lyndir/masterpassword/MPIdenticon.java @@ -1,16 +1,13 @@ package com.lyndir.masterpassword; -import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; +import static com.lyndir.lhunath.opal.system.util.StringUtils.*; -import com.google.common.collect.ImmutableMap; +import com.google.common.base.Charsets; import com.lyndir.lhunath.opal.system.MessageAuthenticationDigests; import com.lyndir.lhunath.opal.system.logging.Logger; -import java.awt.*; import java.nio.*; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.Map; /** @@ -21,20 +18,20 @@ public class MPIdenticon { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MPIdenticon.class ); - private static final Charset charset = StandardCharsets.UTF_8; - private static final Color[] colors = new Color[]{ + private static final Charset charset = Charsets.UTF_8; + private static final Color[] colors = new Color[]{ Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE, Color.MAGENTA, Color.CYAN, Color.MONO }; - private static final char[] leftArm = new char[]{ '╔', '╚', '╰', '═' }; - private static final char[] rightArm = new char[]{ '╗', '╝', '╯', '═' }; - private static final char[] body = new char[]{ '█', '░', '▒', '▓', '☺', '☻' }; - private static final char[] accessory = new char[]{ + private static final char[] leftArm = new char[]{ '╔', '╚', '╰', '═' }; + private static final char[] rightArm = new char[]{ '╗', '╝', '╯', '═' }; + private static final char[] body = new char[]{ '█', '░', '▒', '▓', '☺', '☻' }; + private static final char[] accessory = new char[]{ '◈', '◎', '◐', '◑', '◒', '◓', '☀', '☁', '☂', '☃', '☄', '★', '☆', '☎', '☏', '⎈', '⌂', '☘', '☢', '☣', '☕', '⌚', '⌛', '⏰', '⚡', '⛄', '⛅', '☔', '♔', '♕', '♖', '♗', '♘', '♙', '♚', '♛', '♜', '♝', '♞', '♟', '♨', '♩', '♪', '♫', '⚐', '⚑', '⚔', '⚖', '⚙', '⚠', '⌘', '⏎', '✄', '✆', '✈', '✉', '✌' }; - private final String fullName; - private final Color color; - private final String text; + private final String fullName; + private final Color color; + private final String text; public MPIdenticon(String fullName, String masterPassword) { this( fullName, masterPassword.toCharArray() ); @@ -70,37 +67,13 @@ public class MPIdenticon { return color; } - public enum BackgroundMode { - DARK, LIGHT - } - - public enum Color { - RED( "#dc322f", "#dc322f" ), - GREEN( "#859900", "#859900" ), - YELLOW( "#b58900", "#b58900" ), - BLUE( "#268bd2", "#268bd2" ), - MAGENTA( "#d33682", "#d33682" ), - CYAN( "#2aa198", "#2aa198" ), - MONO( "#93a1a1", "#586e75" ); - - private final String rgbDark; - private final String rgbLight; - - Color(final String rgbDark, final String rgbLight) { - this.rgbDark = rgbDark; - this.rgbLight = rgbLight; - } - - public java.awt.Color getAWTColor(BackgroundMode backgroundMode) { - switch (backgroundMode) { - case DARK: - return new java.awt.Color( Integer.decode( rgbDark ) ); - case LIGHT: - return new java.awt.Color( Integer.decode( rgbLight ) ); - } - - throw new UnsupportedOperationException( "Unsupported background mode: " + backgroundMode ); - } + RED, + GREEN, + YELLOW, + BLUE, + MAGENTA, + CYAN, + MONO } } diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/Res.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/Res.java index a13011d5..d0903dd5 100644 --- a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/Res.java +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/Res.java @@ -1,15 +1,18 @@ package com.lyndir.masterpassword.gui; -import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse; +import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import com.google.common.base.Throwables; import com.google.common.collect.Maps; import com.google.common.io.Resources; -import com.google.common.util.concurrent.*; +import com.google.common.util.concurrent.JdkFutureAdapters; +import com.google.common.util.concurrent.ListenableFuture; import com.lyndir.lhunath.opal.system.logging.Logger; +import com.lyndir.masterpassword.MPIdenticon; import java.awt.*; -import java.awt.event.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.image.ImageObserver; import java.io.IOException; import java.lang.ref.SoftReference; @@ -264,5 +267,72 @@ public abstract class Res { public Color controlBorder() { return controlBorder; } + + public Color fromIdenticonColor(MPIdenticon.Color identiconColor, BackgroundMode backgroundMode) { + switch (identiconColor) { + case RED: + switch (backgroundMode) { + case DARK: + return Color.decode( "#dc322f" ); + case LIGHT: + return Color.decode( "#dc322f" ); + } + break; + case GREEN: + switch (backgroundMode) { + case DARK: + return Color.decode( "#859900" ); + case LIGHT: + return Color.decode( "#859900" ); + } + break; + case YELLOW: + switch (backgroundMode) { + case DARK: + return Color.decode( "#b58900" ); + case LIGHT: + return Color.decode( "#b58900" ); + } + break; + case BLUE: + switch (backgroundMode) { + case DARK: + return Color.decode( "#268bd2" ); + case LIGHT: + return Color.decode( "#268bd2" ); + } + break; + case MAGENTA: + switch (backgroundMode) { + case DARK: + return Color.decode( "#d33682" ); + case LIGHT: + return Color.decode( "#d33682" ); + } + break; + case CYAN: + switch (backgroundMode) { + case DARK: + return Color.decode( "#2aa198" ); + case LIGHT: + return Color.decode( "#2aa198" ); + } + break; + case MONO: + switch (backgroundMode) { + case DARK: + return Color.decode( "#93a1a1" ); + case LIGHT: + return Color.decode( "#586e75" ); + } + break; + } + + throw new IllegalArgumentException( strf( "Color: %s or mode: %s not supported: ", identiconColor, backgroundMode ) ); + } + + public enum BackgroundMode { + DARK, LIGHT + } } } diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/UnlockFrame.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/UnlockFrame.java index a3f3c115..e936bf7e 100644 --- a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/UnlockFrame.java +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/UnlockFrame.java @@ -148,7 +148,7 @@ public class UnlockFrame extends JFrame { else { MPIdenticon identicon = new MPIdenticon( fullName, masterPassword ); identiconLabel.setText( identicon.getText() ); - identiconLabel.setForeground( identicon.getColor().getAWTColor( MPIdenticon.BackgroundMode.DARK ) ); + identiconLabel.setForeground( Res.colors().fromIdenticonColor( identicon.getColor(), Res.Colors.BackgroundMode.DARK ) ); } signInButton.setEnabled( enabled );