From 23f596b97f9be17c73067e6307f94ebacdb63f72 Mon Sep 17 00:00:00 2001 From: srowen Date: Fri, 30 Jan 2009 16:04:03 +0000 Subject: [PATCH] Committed Simon's splash screen for Issue 130 git-svn-id: http://zxing.googlecode.com/svn/trunk@833 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- AUTHORS | 3 +- .../zxing/client/j2me/SplashThread.java | 127 ++++++++++++++++++ .../google/zxing/client/j2me/ZXingMIDlet.java | 17 ++- 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 javame/src/com/google/zxing/client/j2me/SplashThread.java diff --git a/AUTHORS b/AUTHORS index 2a81be8c..1fc738b4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,6 +14,7 @@ Matthew Schulkind (Google) Matt York (LifeMarks) Mohamad Fairol Paul Hackenberger -sanfordsquires (?) +Sanford Squires Sean Owen (Google) +Simon Flannery (Ericsson) Vince Francis (LifeMarks) diff --git a/javame/src/com/google/zxing/client/j2me/SplashThread.java b/javame/src/com/google/zxing/client/j2me/SplashThread.java new file mode 100644 index 00000000..cfee2c23 --- /dev/null +++ b/javame/src/com/google/zxing/client/j2me/SplashThread.java @@ -0,0 +1,127 @@ +/* + * Copyright 2009 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.j2me; + +import javax.microedition.lcdui.Canvas; +import javax.microedition.lcdui.Font; +import javax.microedition.lcdui.Graphics; +import javax.microedition.lcdui.Image; + +/** + *

Any professional software renders a "splash" screen which not only looks + * great, but also keeps a user entertained (and instantly acknowledging the + * user's request to load the application) while important application + * background initialisation takes place.

+ * + * @author Simon Flannery (Ericsson) + */ +class SplashThread extends Canvas implements Runnable { + + private final ZXingMIDlet zXingMIDlet; + private final long tout; + private final Image image; + + /** + * Creates a new Splash Canvas with the given Parent Form and self Time out + * dismissal. The Time out is described in milliseconds. If the Time out is + * assigned Zero, then the Splash Screen will NOT Self dismiss! + * + * When the Splash Screen is dismissed, the splashDone method of the parent form + * is called. + * + * The Splash screen may be dismissed using any of the following procedures: + * (1) The specified timeout has elapsed. (Recommended). If Time out is zero + * however, then the timeout is not taken into consideration and the Splash + * screen simply waits (forever) until notified (see below)! + * (2) By invoking the stop method. (Recommended). This would be used to + * prematurely dismiss the splash screen BEFORE timeout has been reached + * or if a timeout of Zero was given. + * + * @param parent ZXing MIDlet Parent. + * @param timeOut timeout in milliseconds. + * @param splashImage image to display. + */ + SplashThread(ZXingMIDlet parent, long timeOut, Image splashImage) { + zXingMIDlet = parent; + tout = timeOut; + image = splashImage; + setFullScreenMode(true); + new Thread(this).start(); + } + + /** + * Thread Implementation Required. Invoked via calling start method. + * DO NOT manually call this method! + */ + public void run() { + synchronized (this) { + try { + repaint(); + wait(tout); // Let's wake up + } catch (InterruptedException ie) { + // will not occur in MIDP, no thread interrupt method + } + } + + zXingMIDlet.splashDone(); + } + + /** + * Allows Early dismissal of the splash Screen, for example, when all background + * initialisations are complete. + */ + public void stop() { + // Invoke the notify method of the Splash Object ("and the correct thread just + // happens to be arbitrarily chosen as the thread to be awakened"), and thus + // dismiss the Splash Screen. The current implementation only uses a single + // thread, so invoking the notify method should work, however, the + // implementation may change in the future. Thus lets' make use of the + // notifyAll method of the Splash Object. + synchronized (this) { + notifyAll(); // Wake everyone up + } + } + + /** + * Must provide this implementation - used to paint the canvas. + * + * @param g Some Canvas Graphics. + */ + public void paint(Graphics g) { + int width = getWidth(); + int height = getHeight(); + + g.setColor(0x00FFFFFF); + g.fillRect(0, 0, width, height); + + if (image != null) { + g.drawImage(image, width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER); + } + + Font F = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL); + + g.setFont(F); + g.setColor(0x00000000); + + String vendor = zXingMIDlet.getAppProperty("MIDlet-Description"); + + if (vendor != null) { + g.drawString(vendor, width / 2, height - (height / 8), Graphics.BOTTOM | Graphics.HCENTER); + } + } + +} diff --git a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java index f07d8138..84400379 100644 --- a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java +++ b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java @@ -26,6 +26,7 @@ import com.google.zxing.client.result.TelParsedResult; import com.google.zxing.client.result.ProductParsedResult; import com.google.zxing.client.result.URIParsedResult; +import javax.microedition.lcdui.Image; import javax.microedition.io.ConnectionNotFoundException; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; @@ -77,6 +78,9 @@ public final class ZXingMIDlet extends MIDlet { protected void startApp() throws MIDletStateChangeException { try { + Image image = Image.createImage("/res/zxing-icon.png"); + SplashThread splash = new SplashThread(this, 2000, image); + Display.getDisplay(this).setCurrent(splash); player = createPlayer(); player.realize(); MultimediaManager multimediaManager = buildMultimediaManager(); @@ -88,9 +92,6 @@ public final class ZXingMIDlet extends MIDlet { videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas); videoControl.setDisplayLocation(0, 0); videoControl.setDisplaySize(canvas.getWidth(), canvas.getHeight()); - videoControl.setVisible(true); - player.start(); - Display.getDisplay(this).setCurrent(canvas); } catch (IOException ioe) { throw new MIDletStateChangeException(ioe.toString()); } catch (MediaException me) { @@ -109,6 +110,16 @@ public final class ZXingMIDlet extends MIDlet { alert.setTimeout(ALERT_TIMEOUT_MS); } + void splashDone() { + try { + videoControl.setVisible(true); + player.start(); + } catch (MediaException me) { + // continue + } + Display.getDisplay(this).setCurrent(canvas); + } + private static Player createPlayer() throws IOException, MediaException { // Try a workaround for Nokias, which want to use capture://image in some cases Player player = null; -- 2.20.1