Issue 141 -- add flash control for JSR 234 phones
[zxing.git] / javame / src / com / google / zxing / client / j2me / SplashThread.java
1 /*
2  * Copyright 2009 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.j2me;
18
19 import javax.microedition.lcdui.Canvas;
20 import javax.microedition.lcdui.Font;
21 import javax.microedition.lcdui.Graphics;
22 import javax.microedition.lcdui.Image;
23
24 /**
25  * <p>Any professional software renders a "splash" screen which not only looks
26  * great, but also keeps a user entertained (and instantly acknowledging the
27  * user's request to load the application) while important application
28  * background initialisation takes place.</p>
29  *
30  * @author Simon Flannery (Ericsson)
31  */
32 class SplashThread extends Canvas implements Runnable {
33
34   private final ZXingMIDlet zXingMIDlet;
35   private final long tout;
36   private final Image image;
37
38   /**
39    * Creates a new Splash Canvas with the given Parent Form and self Time out
40    * dismissal. The Time out is described in milliseconds. If the Time out is
41    * assigned Zero, then the Splash Screen will NOT Self dismiss!
42    *
43    * When the Splash Screen is dismissed, the splashDone method of the parent form
44    * is called.
45    *
46    * The Splash screen may be dismissed using any of the following procedures:
47    * (1) The specified timeout has elapsed. (Recommended). If Time out is zero
48    * however, then the timeout is not taken into consideration and the Splash
49    * screen simply waits (forever) until notified (see below)!
50    * (2) By invoking the stop method. (Recommended). This would be used to
51    * prematurely dismiss the splash screen BEFORE timeout has been reached
52    * or if a timeout of Zero was given.
53    *
54    * @param parent      ZXing MIDlet Parent.
55    * @param timeOut     timeout in milliseconds.
56    * @param splashImage image to display.
57    */
58   SplashThread(ZXingMIDlet parent, long timeOut, Image splashImage) {
59     zXingMIDlet = parent;
60     tout = timeOut;
61     image = splashImage;
62     setFullScreenMode(true);
63     new Thread(this).start();
64   }
65
66   /**
67    * Thread Implementation Required. Invoked via calling start method.
68    * DO NOT manually call this method!
69    */
70   public void run() {
71     synchronized (this) {
72       try {
73         repaint();
74         wait(tout);  // Let's wake up
75       } catch (InterruptedException ie) {
76         // will not occur in MIDP, no thread interrupt method
77       }
78     }
79
80     zXingMIDlet.splashDone();
81   }
82
83   /**
84    * Allows Early dismissal of the splash Screen, for example, when all background
85    * initialisations are complete.
86    */
87   public void stop() {
88     // Invoke the notify method of the Splash Object ("and the correct thread just
89     // happens to be arbitrarily chosen as the thread to be awakened"), and thus
90     // dismiss the Splash Screen. The current implementation only uses a single
91     // thread, so invoking the notify method should work, however, the
92     // implementation may change in the future. Thus lets' make use of the
93     // notifyAll method of the Splash Object.
94     synchronized (this) {
95       notifyAll(); // Wake everyone up
96     }
97   }
98
99   /**
100    * Must provide this implementation - used to paint the canvas.
101    *
102    * @param g Some Canvas Graphics.
103    */
104   public void paint(Graphics g) {
105     int width = getWidth();
106     int height = getHeight();
107
108     g.setColor(0x00FFFFFF);
109     g.fillRect(0, 0, width, height);
110
111     if (image != null) {
112       g.drawImage(image, width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER);
113     }
114
115     Font F = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
116
117     g.setFont(F);
118     g.setColor(0x00000000);
119
120     String vendor = zXingMIDlet.getAppProperty("MIDlet-Description");
121
122     if (vendor != null) {
123       g.drawString(vendor, width / 2, height - (height / 8), Graphics.BOTTOM | Graphics.HCENTER);
124     }
125   }
126
127 }