Saturday, January 14, 2017

ORM Persistence design for GWT, Android, iOS and J2SE

Our persistence challenge is to have Object Relationship Management (ORM) that works on Android, with GWT RequestFactory, with iOS using j2objc, and using "normal" J2SE for an eventual desktop app.

ORMLite provides a very nice ORM framework that works both using JDBC (e.g. Desktop/Server side java).  GWT requires proxy interfaces so it can manage the back/forth from the server.  SharkORM requires objective c classes.  It all gets very repetitive:

It requires ORM classes like so:

package com.example.helloandroid;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.j256.ormlite.field.DatabaseField;

 * A simple demonstration object we are creating and persisting to the database.
public class SimpleData {

	// id is generated by the database and set on the object automagically
	@DatabaseField(generatedId = true)
	int id;
	@DatabaseField(index = true)
	String string;
	long millis;
	Date date;

	SimpleData() {
		// needed by ormlite

GWT's Request Factory requires a proxy interface that it uses to send data back and forth like so:

public interface SimpleDataProxy extends EntityProxy {

       public int getId();
       public void setId(int id);

       public String getString();
       public void setString(String string);
       public Date getDate();
       public void setDate(Date date);

SharkORM wants something like (haven't tested this yet - but the gist is correct):

//  Header File : SimpleData.h
#import "SharkORM.h"
@interface SimpleData : SRKObject
@property int               id;
@property NSString* string; @property long date;

// Source File : SimpleData.m
#import "Person.h"
@implementation Person
@dynamic id,string,date;

Enter Roaster where you can parse and generate java code.  In NanoLRS (our in progress implementation of an Experience API server designed for embedding in apps) we just make the proxy interface and the rest is generated by roaster by our EntityGeneratorOrmLite class like so:

JavaInterfaceSource proxyInterface = Roaster.parse(JavaInterfaceSource.class, proxyStr);
Iterator<MethodSource<JavaInterfaceSource>> iterator = proxyInterface.getMethods().iterator();

It's a work in progress - but at least it seems like I've found a way around writing the same code slightly differently by hand three times.

Saturday, December 19, 2015

The HTTP dance required make Chrome play video tags

Sometimes in an app you need to use an embedded HTTP server to make HTML content work properly.  In theory you could serve up content from the FileSystem directly: but then HTML5 localstorage, media, cookies, requests for resources from other directories misbehave.  What does cross-domain mean on a filesystem?

In our app we run the excellent NanoHTTPD to serve up content from epub (zipped) files.  Chrome has some (seemingly undocumented) requirements:

  1. You must support HTTP range requests.  You don't need to support multiple ranges: but you must implement returning of 206 partial content in response to a range request.  Interestingly on Android 5.1 Chrome seems to test this by sending a range request for the first byte (0-1).
  2. You must give chrome a means of validating the file remains the same and hasn't changed: A strong (normal) etag works for this.  Setting the last-modified header might work as well.

When using Java inputstreams this of course means using the skip method: be careful that needs to go in a loop because the skip method often does not skill all the bytes requested.  We implemented range support in our EmbeddedHTTPD and RangeInputStream .

Monday, November 30, 2015

The ways around J2ME JSR-135 OutOfMemoryException

Playing media in J2ME seems relatively straightforward: provide an InputStream or address and the media should play.  Unfortunately Nokia's implementation of Manager.createPlayer(InputStream, mimeType) will attempt to buffer the stream in full in memory.  It will always run out of memory because it will attempt to buffer the full media object before playing anything back.  A slower stream just makes you wait a bit before it runs out of memory and doesn't play.

Partial Solution: If you use Manager.createPlayer("file://E:/fileonsdcard.3gp") instead with the jad attribute progressive_download: enabled the file will play smoothly and it won't run out of memory (hoorah!).  But what if the media you want to play isn't just sitting around in raw form on the file system?

I tested out using Manager.createPlayer("http://server/some/file.3gp").  Playing over a 2G or 3G internet connection my half hour 3GP clip played OK.  But using a Nokia Asha 500 (which runs series 40) over a WiFi network the clip only plays successfully one time in 5 or so.  Again seems like the underlying logic thinks "AH!!! Data - let's download everything!!!  Ehh... I ate too much... collapse...".  When I used mod_ratelimit in the apache server I was running on my laptop to limit the speed to 128kbps (16K) behavior returned to more or less what it was with the Internet: Generates an interesting Apache log showing that it downloads a small chunk, then a large chunk, then some smaller chunks.  Towards the end playback started sputtering cutting out for 10-15seconds at a time, playing back for less than a minute, and repeating that cycle. - - [30/Nov/2015:12:59:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 200 6847 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:12:59:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 367505 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:12:59:56 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 200 130375 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1 UNTRUSTED/1.0" - - [30/Nov/2015:13:00:20 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 16860717 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:23:06 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 37080 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:23:42 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 90564 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:24:24 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 58872 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:25:01 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 77460 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:25:39 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 30528 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:26:15 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 147324 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:26:57 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 629964 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:28:09 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 81803 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:28:48 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 30527 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1" - - [30/Nov/2015:13:29:23 +0100] "GET /stream/bbc_click.3gp HTTP/1.1" 206 159739 "-" "Nokia501/2.0 (11.1.1) Profile/MIDP-2.1 Configuration/CLDC-1.1"

Now let's consider some workarounds one might have in mind: and how unfortunately they get thwarted:

  • If I know the duration of a stream: slow the stream down to the rate of playback.  
    • Unfortunately the duration is not available for players created using an InputStream : and the duration is also not available ; so calculating the bitrate would involve manual file reading and codec work.
  • Use a DataSource / SourceStream for Manager.createPlayer
    • I didn't have any luck with this one: I tested my implementation on the emulator with a wav file - was happy.  Tested it on a Series 40 phone: can't get beyond "Error connecting to data source"
  • Let's close the stream at some time to force an interruption / replay
    • Getting this to play in the best of circumstances isn't so much fun... and partial streams (e.g. truncated files) aren't understood
  • Let's slow down the stream to internet like speeds: then use an Internal HTTP Server
    • Well this should produce the same performance as running it over the internet: the problem is the implementation of playing over HTTP misbehaves.
Which leaves us with one final workaround: Write the entire stream to a file and then use Manager.createPlayer with the created file URL.  In our particular implementation we have files downloaded locally but in epub (zipped) files.  Unzipping an 18MB 3gp file (which plays for 30mins) took 2mins on a Nokia 206 (mid range phone).  

For a good overview (that even mentions the let's buffer everything problem) check out this post on InformIT .

Wednesday, November 25, 2015

Using InputStreams with JSR-135 J2ME Sound Playback

In our J2ME app we need to be able to play back sounds for the user for files that are already downloaded (albeit inside zipped epubs).  So connecting the two using Manager.createPlayer(in, mimeType) seems fairly straightforward.  However on the devices themselves with longer files it will croak after a certain amount of time (maybe a buffer under run happens... still not sure).  With some other files that we were accessing for performance and to ensure any active file connections are closed properly we simply read the whole content into a byte array.  Not a good idea when it comes to long mp3 files: here's our recipe that seems to work:

  1. Obtain an inputstream from the file source (in our case, the file source is inside a zip for which we use the gnu classpath project to unzip)
  2. Also from gnu classpath get the BufferedInputStream class (this is not supplied in on J2ME/CLDC devices).
  3. Feed the player a BufferedInputStream instead of the raw stream itself: e.g.
    return new BufferedInputStream(src, 20*1024);
I haven't yet played around with the buffer sizes; occasionally it very briefly misses a fraction of a beat on lower end devices (e.g. the Nokia 110)

Monday, November 9, 2015

J2ME Images stop loading mystery

In our app we need to be able to show images from EPUB files on J2ME .. and those images change as the user goes from page to page.  We're using the rather neat LWUIT HTMLComponent to render the HTML pages with some custom additions to handle media (published here on GitHub).

Mysteriously using a Nokia 206 phone we could load about 19 900x900 pixel jpegs (showing one after the other; with a garbage collect System.gc() call in between) before it would give up - and any subsequent request to load an image would throw an IOException.  This was being caused by an IOException being thrown inside the ResourceThread as it was trying to load the image.  This of course was happening only on the devices: not on the emulator.

J2ME phones is that they vary widely in resolution and memory - from a Nokia 110 which has a 120 pixel or so wide screen to an Asha which has 240x320.  Perhaps images can simply be bounded to a maximum of 320x320 ; or perhaps we need to have different epub files for different phones.  Not sure of that yet.

Note to anyone (still) working with J2ME to avoid tearing hair out : keep images as small as they can be.

Saturday, September 5, 2015

J2ME FileConnection Gotchas: Overwrite and beginning with .

J2ME gives you all the power of what was in a 1997 PC to put apps on billions of phones: with none of the debugging tools.

1. Nokia Series 40 will now allow you to create a filename that begins with "." - do that and you will get an exception .

2. Even in the emulator: if you want to overwrite the entire file when getting an output stream to an existing file: the only way I have seen consistent results is to delete the file first.  Incredibly even openOutputStream(0) - to explicitly open the outputstream at the start seemed to overwrite some but not all of the file (e.g. if you want to overwrite with a smaller file than it was originally there will be trailing leftovers from the previous file).  Something like this works:

public OutputStream openFileOutputStream(String fileURI, boolean autocreate) throws IOException{
        FileConnection con = null;
        IOException e = null;
        try {
            con = (FileConnection), Connector.READ_WRITE);
            if(con.exists()) {
        }catch(IOException e2) {
            e = e2;
        }finally {
            if(e != null) throw e;
        OutputStream out = Connector.openOutputStream(fileURI);
        return out;        

Friday, August 28, 2015

No Java 8: No JSON for you says Android: UnsupportedClassVersionError: org/json/JSONObject

Let's say you are crazy enough to be running the Ubuntu Long Term Support distribution.  Maybe you would even rather like to use the jdk package from the Ubuntu repo so it gets automatically updated?  Android says: No; not if you feel like using JSON.

java.lang.UnsupportedClassVersionError: org/json/JSONObject : Unsupported major.minor version 52.0

No you won't be able to just change the JSON version in Maven like others who get stuck with it.  You will upgrade to JDK 8 or your code will not run.  Developer's using JSON: wow, what an edge case, eh?  Who would possibly expect Google to conduct enough testing to figure that one out and say "Hey - you have to use JDK 8 to compile from now on".