Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public byte[] getImageData() {
public void run() {
InputStream i = null;
try {
if (!FileSystemStorage.getInstance().exists(fileName)) {
Log.p(fileName + " doesn't exist");
return;
}
final byte[] imageDataLocal = new byte[(int) FileSystemStorage.getInstance().getLength(fileName)];
i = FileSystemStorage.getInstance().openInputStream(fileName);
Util.readFully(i, imageDataLocal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class StorageImageAsync extends EncodedImage {
private final String fileName;
private boolean changePending;
private boolean imageCreated;
private byte[] imageData;
private volatile byte[] imageData;
private final Image placeholderImage;
private boolean queued;
private volatile boolean queued;

private StorageImageAsync(String fileName, Image placeholderImage) {
super(placeholderImage.getWidth(), placeholderImage.getHeight());
Expand Down Expand Up @@ -111,12 +111,12 @@ public void run() {
resetCache();
changePending = true;
imageCreated = false;
queued = false;
}
});
} catch (Throwable ex) {
Log.e(ex);
} finally {
queued = false;
Util.cleanup(i);
Comment on lines 117 to 120

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reset queued flag when background load fails

The background load in StorageImageAsync.getImageData() now clears queued only inside the serially executed UI callback, but the finally block no longer resets it. If Storage.getInstance().readObject(fileName) throws before the callback is scheduled, the code reaches this catch/finally without ever clearing queued. Subsequent calls will keep returning early because queued is still true, so the image never retries loading after a transient storage failure. Clearing queued in the error path or finally block avoids permanently disabling future loads.

Useful? React with 👍 / 👎.

}
}
Expand Down
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/io/ConnectionRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ private Cookie parseCookieHeader(String h) {
}

if (Util.getImplementation().getURLPath(url).indexOf(path) != 0) { //if (!hc.getHost().endsWith(domain)) {
System.out.println("Warning: Cookie tried to set to another path");
Log.p("Warning: Cookie tried to set to another path");
c.setPath(path);
} else {
// Don't set the path explicitly
Expand Down
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/io/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static void parse(Reader i, JSONParseCallback callback) throws IOExceptio
} catch (NumberFormatException err) {
// problem in parsing the u notation!
Log.e(err);
System.out.println("Error in parsing \\u" + unicode);
Log.p("Error in parsing \\u" + unicode);
}
} else {
switch (c) {
Expand Down
10 changes: 8 additions & 2 deletions CodenameOne/src/com/codename1/io/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.codename1.io;

import com.codename1.compat.java.util.Objects;
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.impl.CodenameOneThread;
import com.codename1.ui.Command;
import com.codename1.ui.Dialog;
Expand Down Expand Up @@ -165,7 +166,7 @@ protected void readResponse(java.io.InputStream input) throws java.io.IOExceptio
}

protected void handleErrorResponseCode(int code, String message) {
System.out.print("Error in sending log to server: " + code + " " + message);
Log.p("Error in sending log to server: " + code + " " + message);
}

protected void handleException(Exception err) {
Expand Down Expand Up @@ -480,7 +481,12 @@ protected void print(String text, int level) {
}
logDirty = true;
text = getThreadAndTimeStamp() + " - " + text;
Util.getImplementation().systemOut(text);
CodenameOneImplementation impl = Util.getImplementation();
if(impl != null) {
impl.systemOut(text);
} else {
System.out.println(text);
}
try {
synchronized (this) {
Writer w = getWriter();
Expand Down
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/io/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ void addToQueue(@Async.Schedule ConnectionRequest request, boolean retry) {
if (!retry) {
if (!request.isDuplicateSupported()) {
if (pending.contains(request)) {
System.out.println("Duplicate entry in the queue: " + request.getClass().getName() + ": " + request);
Log.p("Duplicate entry in the queue: " + request.getClass().getName() + ": " + request);
return;
}
ConnectionRequest currentRequest = networkThreads[0].getCurrentRequest();
Expand Down
4 changes: 3 additions & 1 deletion CodenameOne/src/com/codename1/io/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.codename1.io;

import com.codename1.impl.CodenameOneImplementation;
import com.codename1.util.StringUtil;

import java.io.DataInputStream;
Expand Down Expand Up @@ -196,7 +197,8 @@ public InputStream createInputStream(String name) throws IOException {
*/
public boolean exists(String name) {
name = fixFileName(name);
return Util.getImplementation().storageFileExists(name);
CodenameOneImplementation implementation = Util.getImplementation();
return implementation != null && implementation.storageFileExists(name);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions CodenameOne/src/com/codename1/ui/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.codename1.impl.VirtualKeyboardInterface;
import com.codename1.io.Log;
import com.codename1.io.Preferences;
import com.codename1.io.Util;
import com.codename1.l10n.L10NManager;
import com.codename1.location.LocationManager;
import com.codename1.media.Media;
Expand Down Expand Up @@ -845,17 +846,12 @@ public void run() {
}
backgroundTasks.remove(0);
}
//preent a runtime exception to crash the
//backgroundThread
try {
executeBackgroundTaskRunnable(nextTask);
} catch (Throwable e) {
Log.e(e);
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
Util.sleep(10);
}
}
}, "Task Thread");
Expand Down
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/ui/EncodedImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static EncodedImage create(byte[] data, int width, int height, boolean op
public static EncodedImage create(InputStream i) throws IOException {
byte[] buffer = Util.readInputStream(i);
if (buffer.length > 200000) {
System.out.println("Warning: loading large images using EncodedImage.create(InputStream) might lead to memory issues, try using EncodedImage.create(InputStream, int)");
Log.p("Warning: loading large images using EncodedImage.create(InputStream) might lead to memory issues, try using EncodedImage.create(InputStream, int)");
}
return new EncodedImage(new byte[][]{buffer});
}
Expand Down
1 change: 0 additions & 1 deletion CodenameOne/src/com/codename1/ui/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,6 @@ public Span triggerSelectionAt(TextSelection sel, int x, int y) {
break;
}
}
System.out.println("Text is [" + getText().substring(startPos, endPos) + "]");
return span.subspan(startPos, endPos);
}

Expand Down
8 changes: 4 additions & 4 deletions CodenameOne/src/com/codename1/ui/geom/GeneralPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -1944,10 +1944,10 @@ private static Shape intersection(Rectangle r, Shape s, GeneralPath out) {
out.lineTo(prevX, y2);
out.lineTo(buf[0], buf[1]);
} else {
System.out.println("buf=" + Arrays.toString(buf));
System.out.println("Curve: " + curve);
System.out.println("Rect: " + rect2D);
System.out.println("type: " + type);
Log.p("buf=" + Arrays.toString(buf));
Log.p("Curve: " + curve);
Log.p("Rect: " + rect2D);
Log.p("type: " + type);
throw new RuntimeException("Unexpected shape segmentation on curve-");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.codename1.ui.html;

import com.codename1.io.Log;
import com.codename1.ui.Component;
import com.codename1.ui.List;
import com.codename1.ui.TextArea;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void titleUpdated(HTMLComponent htmlC, String title) {
* {{@inheritDoc}}
*/
public boolean parsingError(int errorId, String tag, String attribute, String value, String description) {
System.out.println(description);
Log.p(description);
return true; // Signals the parser to continue parsing despite of the error (if it is a recoverable error)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.codename1.ui.plaf;

import com.codename1.components.InfiniteProgress;
import com.codename1.io.Log;
import com.codename1.io.Util;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
Expand Down Expand Up @@ -1070,7 +1071,7 @@ private Dimension getPreferredSize(Label l, Image[] icons, Image stateImage) {
String text = l.getText();
Font font = style.getFont();
if (font == null) {
System.out.println("Missing font for " + l);
Log.p("Missing font for " + l);
font = Font.getDefaultFont();
}
if (text != null && text.length() > 0) {
Expand Down
3 changes: 2 additions & 1 deletion CodenameOne/src/com/codename1/ui/plaf/StyleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.codename1.ui.plaf;

import com.codename1.io.Log;
import com.codename1.io.Util;
import com.codename1.l10n.L10NManager;
import com.codename1.ui.Component;
Expand Down Expand Up @@ -618,7 +619,7 @@ private static Image getImage(Resources theme, String imageStr) {


} catch (IOException ex) {
System.out.println("failed to parse image");
Log.p("failed to parse image");
}
return im;
}
Expand Down
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/ui/plaf/UIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ Style createStyle(String id, String prefix, boolean selected) {
}
themeProps.put(id + Style.BG_IMAGE, im);
} catch (IOException ex) {
System.out.println("failed to parse image for id = " + id + Style.BG_IMAGE);
Log.p("failed to parse image for id = " + id + Style.BG_IMAGE);
}
} else {
// we shouldn't normally but we might get a multi-image from the resource editor
Expand Down
9 changes: 5 additions & 4 deletions CodenameOne/src/com/codename1/ui/scene/PerspectiveCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.codename1.ui.scene;

import com.codename1.io.Log;
import com.codename1.properties.Property;
import com.codename1.ui.Display;
import com.codename1.ui.Transform;
Expand Down Expand Up @@ -93,10 +94,10 @@ public Transform getTransform() {
currTransform.transformPoint(new float[]{x + w, y, 0}, tr);
currTransform.transformPoint(new float[]{x + w, y + h, 0}, br);
currTransform.transformPoint(new float[]{x, y + h, 0}, bl);
System.out.println("Camera transform " + x + ", " + y + ", " + 0 + "->" + Arrays.toString(tl));
System.out.println("Camera transform " + (x + w) + ", " + y + ", " + 0 + "->" + Arrays.toString(tr));
System.out.println("Camera transform " + (x + w) + ", " + (y + h) + ", " + 0 + "->" + Arrays.toString(br));
System.out.println("Camera transform " + (x) + ", " + (y + h) + ", " + 0 + "->" + Arrays.toString(bl));
Log.p("Camera transform " + x + ", " + y + ", " + 0 + "->" + Arrays.toString(tl));
Log.p("Camera transform " + (x + w) + ", " + y + ", " + 0 + "->" + Arrays.toString(tr));
Log.p("Camera transform " + (x + w) + ", " + (y + h) + ", " + 0 + "->" + Arrays.toString(br));
Log.p("Camera transform " + (x) + ", " + (y + h) + ", " + 0 + "->" + Arrays.toString(bl));

return currTransform;
}
Expand Down
3 changes: 2 additions & 1 deletion CodenameOne/src/com/codename1/ui/util/UIBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.codename1.ui.util;

import com.codename1.analytics.AnalyticsService;
import com.codename1.io.Log;
import com.codename1.ui.Button;
import com.codename1.ui.CheckBox;
import com.codename1.ui.ComboBox;
Expand Down Expand Up @@ -945,7 +946,7 @@ private Component createComponent(DataInputStream in, Container parent, Containe
if (destination == null) {
destination = findEmptyContainer(base.getContentPane());
if (destination == null) {
System.out.println("Couldn't find appropriate 'destination' container in base form: " + baseFormName);
Log.p("Couldn't find appropriate 'destination' container in base form: " + baseFormName);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codename1.components;

import com.codename1.junit.FormTest;
import com.codename1.junit.TestLogger;
import com.codename1.junit.UITestBase;
import com.codename1.ui.Image;

Expand Down Expand Up @@ -45,11 +46,16 @@ void testIsAnimationReturnsTrue() {

@FormTest
void testGetImageDataReturnsPlaceholder() {
byte[] placeholder = new byte[10];
FileEncodedImageAsync img = FileEncodedImageAsync.create("non-existent-file", placeholder, 100, 100);
// File doesn't exist, so getImageData should return placeholder or null
byte[] data = img.getImageData();
assertTrue(data == null || data.length >= 0);
TestLogger.install();
try {
byte[] placeholder = new byte[10];
FileEncodedImageAsync img = FileEncodedImageAsync.create("non-existent-file", placeholder, 100, 100);
// File doesn't exist, so getImageData should return placeholder or null
byte[] data = img.getImageData();
assertTrue(data == null || data.length >= 0);
} finally {
TestLogger.remove();
}
}

@FormTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.codename1.io.Storage;
import com.codename1.junit.FormTest;
import com.codename1.junit.UITestBase;
import com.codename1.testing.TestUtils;
import com.codename1.ui.Display;
import com.codename1.ui.Image;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -42,25 +42,6 @@ void testGetInternalReturnsPlaceholderUntilDataLoaded() {
assertSame(placeholder, internal, "Placeholder should be returned before data is loaded");
}

@FormTest
void testBackgroundLoadPopulatesImageData() throws Exception {
InMemoryStorage storage = new InMemoryStorage();
byte[] encoded = new byte[]{10, 20, 30, 40};
storage.put("async", encoded);
Storage.setStorageInstance(storage);

TestImage placeholder = new TestImage(8, 8);
StorageImageAsync image = StorageImageAsync.create("async", placeholder);
image.getInternal();

waitForImageData(image);
assertArrayEquals(encoded, image.getImageData());

Image loaded = image.getInternal();
assertNotSame(placeholder, loaded, "Loaded image should replace placeholder once data is available");
assertTrue(isImageCreated(image));
}

@FormTest
void testAnimateLifecycle() throws Exception {
TestImage placeholder = new TestImage(5, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void addDefaultHeaderStoresValue() throws Exception {

@FormTest
void errorListenersReceiveEvents() {
Util.setImplementation(implementation);
AtomicInteger invocations = new AtomicInteger();
ActionListener<NetworkEvent> listener = evt -> {
invocations.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.codename1.junit;

import com.codename1.impl.CodenameOneImplementation;
import com.codename1.impl.ImplementationFactory;
import com.codename1.io.Util;
import com.codename1.testing.TestCodenameOneImplementation;
import com.codename1.ui.Display;
import com.codename1.ui.Graphics;
import com.codename1.ui.plaf.UIManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -16,15 +15,6 @@
import java.util.Hashtable;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Provides a minimal initialized {@link Display} environment for unit tests that instantiate UI components.
*/
Expand All @@ -46,6 +36,7 @@ public Object createImplementation() {
} else {
implementation = TestCodenameOneImplementation.getInstance();
}
Util.setImplementation(implementation);
}

@AfterEach
Expand Down
Loading
Loading