-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/dorkbox/Notify Use: notifier.implementation = notify notifier.notify.position = CENTER (TOP_RIGHT by default) notifier.notify.darkstyle = true (false by default)
- Loading branch information
Showing
9 changed files
with
263 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
...notification/src/main/java/fr/jcgay/notification/notifier/notify/NotifyConfiguration.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package fr.jcgay.notification.notifier.notify; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import dorkbox.notify.Pos; | ||
|
||
import java.util.Properties; | ||
|
||
@AutoValue | ||
public abstract class NotifyConfiguration { | ||
|
||
private static final NotifyConfiguration DEFAULT = new AutoValue_NotifyConfiguration(Pos.TOP_RIGHT, false); | ||
|
||
public abstract Pos position(); | ||
|
||
public abstract boolean withDarkStyle(); | ||
|
||
NotifyConfiguration() { | ||
// prevent external subclasses | ||
} | ||
|
||
public static NotifyConfiguration byDefault() { | ||
return DEFAULT; | ||
} | ||
|
||
public static NotifyConfiguration create(Properties properties) { | ||
if (properties == null) { | ||
return byDefault(); | ||
} | ||
|
||
return new AutoValue_NotifyConfiguration( | ||
Pos.valueOf(properties.getProperty("notifier.notify.position", DEFAULT.position().name())), | ||
Boolean.valueOf(properties.getProperty("notifier.notify.darkstyle", String.valueOf(DEFAULT.withDarkStyle()))) | ||
); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
send-notification/src/main/java/fr/jcgay/notification/notifier/notify/NotifyNotifier.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package fr.jcgay.notification.notifier.notify; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.base.Objects; | ||
import dorkbox.notify.Notify; | ||
import fr.jcgay.notification.Application; | ||
import fr.jcgay.notification.DiscoverableNotifier; | ||
import fr.jcgay.notification.Notification; | ||
import fr.jcgay.notification.Notifier; | ||
import org.slf4j.Logger; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class NotifyNotifier implements DiscoverableNotifier { | ||
|
||
private static final Logger LOGGER = getLogger(NotifyNotifier.class); | ||
|
||
private final Application application; | ||
private final NotifyConfiguration configuration; | ||
|
||
private boolean skipNotifications; | ||
|
||
public NotifyNotifier(Application application, NotifyConfiguration configuration) { | ||
LOGGER.debug("Configuring notify: {}.", configuration); | ||
this.configuration = configuration; | ||
this.application = application; | ||
} | ||
|
||
@Override | ||
public Notifier init() { | ||
if (isHeadless()) { | ||
skipNotifications = true; | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean tryInit() { | ||
init(); | ||
return !skipNotifications; | ||
} | ||
|
||
@Override | ||
public void send(Notification notification) { | ||
Notify notify = Notify.create() | ||
.title(notification.title()) | ||
.text(notification.message()) | ||
.graphic(notification.icon().toImage()) | ||
.position(configuration.position()) | ||
.hideAfter((int) (application.timeout() == -1 ? SECONDS.toMillis(3) : application.timeout())); | ||
|
||
if (configuration.withDarkStyle()) { | ||
notify.darkStyle(); | ||
} | ||
|
||
notify.show(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!skipNotifications) { | ||
try { | ||
Thread.sleep(application.timeout() == -1 ? SECONDS.toMillis(3) : application.timeout()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
private static boolean isHeadless() { | ||
return "true".equals(System.getProperty("java.awt.headless")); | ||
} | ||
|
||
@Override | ||
public boolean isPersistent() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(application, configuration, skipNotifications); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
final NotifyNotifier other = (NotifyNotifier) obj; | ||
return Objects.equal(this.application, other.application) | ||
&& Objects.equal(this.configuration, other.configuration) | ||
&& Objects.equal(this.skipNotifications, other.skipNotifications); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("application", application) | ||
.add("configuration", configuration) | ||
.add("skipNotifications", skipNotifications) | ||
.toString(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...tion/src/test/groovy/fr/jcgay/notification/notifier/notify/NotifyConfigurationSpec.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package fr.jcgay.notification.notifier.notify | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import static dorkbox.notify.Pos.* | ||
|
||
class NotifyConfigurationSpec extends Specification { | ||
|
||
@Unroll | ||
def "should build default configuration when properties are [#empty]"() { | ||
when: | ||
def result = NotifyConfiguration.create(empty) | ||
|
||
then: | ||
result == NotifyConfiguration.byDefault() | ||
|
||
where: | ||
empty << [null, new Properties()] | ||
} | ||
|
||
@Unroll | ||
def "should build user configuration with position [#position]"() { | ||
when: | ||
def result = NotifyConfiguration.create( | ||
['notifier.notify.position': position.name()] as Properties | ||
) | ||
|
||
then: | ||
result.position() == position | ||
result.withDarkStyle() == NotifyConfiguration.byDefault().withDarkStyle() | ||
|
||
where: | ||
position << [TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, CENTER] | ||
} | ||
|
||
@Unroll | ||
def "should build user configuration with dark style ? [#style]"() { | ||
when: | ||
def result = NotifyConfiguration.create( | ||
['notifier.notify.darkstyle': style as String] as Properties | ||
) | ||
|
||
then: | ||
result.position() == NotifyConfiguration.byDefault().position() | ||
result.withDarkStyle() == style | ||
|
||
where: | ||
style << [true, false] | ||
} | ||
|
||
def "fail when position is not valid"() { | ||
when: | ||
NotifyConfiguration.create( | ||
['notifier.notify.position': 'unknown'] as Properties | ||
) | ||
|
||
then: | ||
thrown(IllegalArgumentException) | ||
} | ||
|
||
def "get lighter style when darkstyle is not a boolean"() { | ||
when: | ||
def result = NotifyConfiguration.create( | ||
['notifier.notify.darkstyle': 'maybe'] as Properties | ||
) | ||
|
||
then: | ||
!result.withDarkStyle() | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fr.jcgay.notification.Application; | ||
import fr.jcgay.notification.Icon; | ||
import fr.jcgay.notification.Notification; | ||
import fr.jcgay.notification.Notifier; | ||
import fr.jcgay.notification.SendNotification; | ||
|
||
import java.net.URL; | ||
|
||
public class NotifyExample { | ||
|
||
public static void main(String[] args) { | ||
URL icon = SystemTrayExample.class.getResource("/image/dialog-clean.png"); | ||
|
||
Application application = Application.builder() | ||
.id("notify-example") | ||
.name("Notify Example") | ||
.icon(Icon.create(icon, "app")) | ||
.build(); | ||
|
||
Notifier notifier = new SendNotification() | ||
.setApplication(application) | ||
.setChosenNotifier("notify") | ||
.initNotifier(); | ||
|
||
Notification notification = Notification.builder() | ||
.title("Notify Notification") | ||
.message("Hello !") | ||
.icon(Icon.create(icon, "ok")) | ||
.build(); | ||
|
||
notifier.send(notification); | ||
notifier.close(); | ||
} | ||
} |