001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.lcdui;
027:
028: import javax.microedition.lcdui.Display;
029: import java.util.Timer;
030: import java.util.TimerTask;
031:
032: /**
033: * Class that allows Display to access
034: * Device specific calls in the native code
035: * like flashBacklight.
036: */
037:
038: public class DisplayDeviceAccess {
039:
040: /**
041: * The Timer to service TimerTasks.
042: */
043: private static Timer timerService = new Timer();
044:
045: /**
046: * A TimerTask.
047: */
048: private TimerTask task = null;
049:
050: /**
051: * The interval, in microseconds between backlight
052: * toggles (in microseconds)
053: */
054: private static int BLINK_RATE = 250;
055:
056: /**
057: * Number of repetitions left in flash duration
058: */
059: private int flashCount = 0;
060:
061: /**
062: * We always want this to be false at the end of
063: * a flashBacklight call.
064: */
065: private boolean isLit = false;
066:
067: /**
068: * Requests a flashing effect for the device's backlight.
069: *
070: * @param displayId The display ID associated with this Display
071: * @param duration the number of milliseconds the backlight should be
072: * flashed, or zero if the flashing should be stopped
073: *
074: * @return true if the backlight can be controlled
075: */
076: public boolean flashBacklight(int displayId, int duration) {
077:
078: // Test for negative of duration is in public class
079:
080: if (duration == 0) {
081: cancelTimer();
082: if (isLit) {
083: isLit = !isLit;
084: return toggleBacklight0(displayId);
085: } else {
086: return isBacklightSupported0(displayId);
087: }
088: } else {
089: setTimer(displayId, duration);
090: isLit = !isLit;
091: return toggleBacklight0(displayId);
092: }
093: }
094:
095: /**
096: * Set a new timer. Determine <code>flashCount</code> based on
097: * <code>duration</code> divided by <code>BLINK_RATE</code>
098: *
099: * @param displayId The display ID associated with this Display
100: * @param duration the number of milliseconds the timer should be run
101: */
102: private void setTimer(int displayId, int duration) {
103: cancelTimer();
104: try {
105: task = new TimerClient(displayId);
106: /* flash every <tt>BLINK_RATE</tt> miliseconds */
107: flashCount = duration / BLINK_RATE;
108: timerService.schedule(task, BLINK_RATE, BLINK_RATE);
109: } catch (IllegalStateException e) {
110: cancelTimer();
111: }
112: }
113:
114: /**
115: * Cancel any running Timer.
116: */
117: private void cancelTimer() {
118: if (task != null) {
119: task.cancel();
120: task = null;
121: }
122: flashCount = 0;
123: }
124:
125: /**
126: * Inner class TimerTask
127: *
128: * Used to toggle the backlight and turn it off when
129: * the duration of the timer is up
130: */
131: class TimerClient extends TimerTask {
132:
133: /**
134: * Creates TimerClient to show backlight for
135: * Display with passed displayId.
136: *
137: * @param displayId The display ID associated with the caller Display
138: */
139: TimerClient(int displayId) {
140: this .displayId = displayId;
141: }
142:
143: /**
144: * If there are flashes left to perform,
145: * simply toggle the backlight.
146: */
147: public final void run() {
148: if (flashCount > 0) {
149: flashCount--;
150: isLit = !isLit;
151: toggleBacklight0(displayId);
152: } else {
153: if (isLit) {
154: isLit = !isLit;
155: toggleBacklight0(displayId);
156: }
157: this .cancel();
158: }
159: }
160:
161: /** The display ID associated with the caller Display */
162: private int displayId;
163: }
164:
165: /**
166: * Toggles backlight.
167: *
168: * @param displayId The display ID associated with the caller Display
169: * @return true if backlight control is supported, false otherwise
170: */
171: private native boolean toggleBacklight0(int displayId);
172:
173: /**
174: * Tests if backlight is supported.
175: *
176: * @param displayId The display ID associated with the caller Display
177: * @return true if backlight control is supported, false otherwise
178: */
179: private native boolean isBacklightSupported0(int displayId);
180:
181: }
|