Skip to content

Commit a617c29

Browse files
committed
Optimizations
1 parent a17adee commit a617c29

File tree

3 files changed

+27
-49
lines changed

3 files changed

+27
-49
lines changed

src/main/java/mpo/dayon/assisted/utils/ScreenUtilities.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public final class ScreenUtilities {
2222

2323
private static Rectangle sharedScreenSize;
2424

25-
private static int[] rgb;
26-
2725
private static byte[] gray;
2826

2927
private static boolean shareAllScreens;
@@ -35,23 +33,22 @@ private ScreenUtilities() {
3533
NUMBER_OF_SCREENS = countScreens();
3634
DEFAULT_SIZE = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
3735
COMBINED_SCREEN_SIZE = getCombinedScreenSize();
38-
sharedScreenSize = shareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
39-
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
40-
gray = new byte[rgb.length];
36+
init();
4137
try {
4238
ROBOT = new Robot();
4339
} catch (AWTException ex) {
4440
throw new IllegalStateException("Could not initialize the AWT robot!", ex);
4541
}
4642
}
4743

48-
public static void setShareAllScreens(boolean doShareAllScreens) {
49-
synchronized (ScreenUtilities.class) {
50-
shareAllScreens = doShareAllScreens;
51-
sharedScreenSize = doShareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
52-
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
53-
gray = new byte[rgb.length];
54-
}
44+
public static synchronized void setShareAllScreens(boolean doShareAllScreens) {
45+
shareAllScreens = doShareAllScreens;
46+
init();
47+
}
48+
49+
private static void init() {
50+
sharedScreenSize = shareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
51+
gray = new byte[sharedScreenSize.height * sharedScreenSize.width];
5552
}
5653

5754
public static Rectangle getSharedScreenSize() {
@@ -68,8 +65,9 @@ private static int countScreens() {
6865

6966
private static Rectangle getCombinedScreenSize() {
7067
Rectangle fullSize = new Rectangle();
71-
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
72-
stream(environment.getScreenDevices()).flatMap(gd -> stream(gd.getConfigurations())).forEach(graphicsConfiguration -> Rectangle2D.union(fullSize, graphicsConfiguration.getBounds(), fullSize));
68+
stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
69+
.flatMap(gd -> stream(gd.getConfigurations()))
70+
.forEach(graphicsConfiguration -> Rectangle2D.union(fullSize, graphicsConfiguration.getBounds(), fullSize));
7371
return fullSize.getBounds();
7472
}
7573

src/main/java/mpo/dayon/common/capture/CaptureEngineConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public int hashCode() {
7878
*/
7979
@Override
8080
protected void persist(boolean clear) {
81-
final Preferences.Props props = getProps(clear);
82-
Preferences.getPreferences().update(props); // atomic (!)
81+
Preferences.getPreferences().update(getProps(clear)); // atomic (!)
8382
}
8483

8584
private Preferences.Props getProps(boolean clear) {

src/main/java/mpo/dayon/common/preference/Preferences.java

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,12 @@ public static synchronized Preferences getPreferences() {
5757
}
5858

5959
try {
60-
final Preferences xpreferences;
6160
final File file = getOrCreatePreferencesFile();
62-
63-
if (file.exists()) {
64-
Log.info("Preferences (existing) [" + file.getAbsolutePath() + "]");
65-
} else {
66-
Log.info("Preferences (new) [" + file.getAbsolutePath() + "]");
67-
}
68-
xpreferences = new Preferences(file);
69-
setupPersister(xpreferences);
70-
preferences = xpreferences;
71-
return preferences;
61+
Log.info("Preferences (" + (file.exists() ? "existing" : "new") + ") [" + file.getAbsolutePath() + "]");
62+
return preferences = setupPersister(new Preferences(file));
7263
} catch (IOException ex) {
7364
Log.warn("Preferences get/create error!", ex);
74-
preferences = NULL;
75-
return preferences;
65+
return preferences = NULL;
7666
}
7767
}
7868

@@ -180,41 +170,32 @@ public void update(Props props) {
180170

181171
/**
182172
* Some components are possibly sending a lot of updates (e.g., main frame
183-
* resize) and it makes no sense to write every changes as we want the last
173+
* resize) and it makes no sense to write every change as we want the last
184174
* one only => I'm polling instead of saving each time a value has changed
185175
* ...
186176
*/
187-
private static void setupPersister(final Preferences preferences) {
177+
private static Preferences setupPersister(final Preferences preferences) {
188178
new Timer("PreferencesWriter").schedule(new TimerTask() {
189179
@Override
190180
public void run() {
191181
if (preferences.isNull() || preferences.getWriteError().get()) {
192182
return;
193183
}
194184

195-
try {
196-
Properties cloned = null;
197-
synchronized (preferences.getCloneLOCK()) {
198-
if (preferences.getDirty().get()) {
199-
cloned = (Properties) preferences.getProps().clone();
200-
preferences.getDirty().set(false);
201-
}
202-
}
203-
if (cloned != null) {
204-
Log.debug("Writing the preferences [%s]", () -> preferences.getFile().getAbsolutePath());
185+
synchronized (preferences.getCloneLOCK()) {
186+
if (preferences.getDirty().get()) {
205187
try (PrintWriter out = new PrintWriter(preferences.getFile(), UTF_8)) {
206-
cloned.store(out, null);
207-
out.flush();
188+
Log.debug("Writing the preferences [%s]", () -> preferences.getFile().getAbsolutePath());
189+
preferences.getProps().store(out, null);
190+
preferences.getDirty().set(false);
191+
} catch (IOException ex) {
192+
Log.error("Preferences write error!", ex);
193+
preferences.getWriteError().set(true);
208194
}
209195
}
210-
} catch (FileNotFoundException e) {
211-
Log.error("Preferences (write) permission denied");
212-
preferences.getWriteError().set(true);
213-
} catch (IOException ex) {
214-
Log.error("Preferences write error!", ex);
215-
preferences.getWriteError().set(true);
216196
}
217197
}
218198
}, 0, 2000);
199+
return preferences;
219200
}
220201
}

0 commit comments

Comments
 (0)