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:
027: package com.sun.midp.suspend;
028:
029: import com.sun.midp.main.*;
030: import com.sun.midp.security.SecurityToken;
031: import com.sun.midp.lcdui.DisplayEventHandlerFactory;
032: import com.sun.midp.lcdui.DisplayEventHandler;
033: import com.sun.midp.lcdui.SystemAlert;
034: import com.sun.midp.i18n.Resource;
035: import com.sun.midp.i18n.ResourceConstants;
036:
037: import javax.microedition.lcdui.AlertType;
038: import javax.microedition.lcdui.CommandListener;
039: import javax.microedition.lcdui.Displayable;
040: import javax.microedition.lcdui.Command;
041: import java.util.Timer;
042: import java.util.TimerTask;
043:
044: /**
045: * User interface utilities for suspend/resume subsystem.
046: */
047: class SuspendResumeUI {
048: /**
049: * Alert notifying user of system suspension. It is null if the alert
050: * is not being shown currently.
051: */
052: private static SystemAlert suspendAlert;
053:
054: /**
055: * Shows alert notifying user of system suspend.
056: * @param token security token for accessing restricted API
057: */
058: static synchronized void showSuspendAlert(final SecurityToken token) {
059: if (null == suspendAlert && AlertTimer.shouldShow()) {
060: String title = Resource.getString(
061: ResourceConstants.SR_SUSPEND_ALERT_TITLE, null);
062: String message = Resource.getString(
063: ResourceConstants.SR_SUSPEND_ALERT_MSG, null);
064:
065: AlertTimer.start();
066:
067: CommandListener ignoring = new CommandListener() {
068: public void commandAction(Command c, Displayable d) {
069: }
070: };
071:
072: suspendAlert = new SystemAlert(getDisp(token), title,
073: message, null, AlertType.WARNING);
074: suspendAlert.setCommandListener(ignoring);
075:
076: suspendAlert.runInNewThread();
077: }
078: }
079:
080: /**
081: * Dismisses alert notifying user of system suspend.
082: */
083: static synchronized void dismissSuspendAlert() {
084: if (null != suspendAlert) {
085: suspendAlert.dismiss();
086: suspendAlert = null;
087: }
088: }
089:
090: /**
091: * Shows alert notifying user that all MIDlets were killed during
092: * preceding system suspension.
093: * @param token security token for accessing restricted API
094: */
095: static void showAllKilledAlert(SecurityToken token) {
096: String title = Resource.getString(
097: ResourceConstants.SR_ALL_KILLED_ALERT_TITLE, null);
098: String message = Resource.getString(
099: ResourceConstants.SR_ALL_KILLED_ALERT_MSG, null);
100:
101: SystemAlert alert = new SystemAlert(getDisp(token), title,
102: message, null, AlertType.WARNING);
103: alert.runInNewThread();
104: }
105:
106: /**
107: * Retrieves a display handler that guarantees exposition of a
108: * system alert.
109: * @param token security token for accessing restricted API
110: * @return DisplayEventHandler instance for alert exposition
111: */
112: private static DisplayEventHandler getDisp(SecurityToken token) {
113: /* IMPL_NOTE: Due to current implementation of display preempting,
114: * alert is shown immediately only if launched from the isolate
115: * that has foreground. Moving AMS isolate to foreground to
116: * get display preemption.
117: */
118: MIDletProxyList proxyList = MIDletProxyList
119: .getMIDletProxyList(token);
120: proxyList.setForegroundMIDlet(proxyList.findAmsProxy());
121:
122: return DisplayEventHandlerFactory.getDisplayEventHandler(token);
123: }
124: }
125:
126: /**
127: * Alert timer that guarantees exposition of suspend alert.
128: */
129: class AlertTimer extends Timer implements SuspendDependency {
130: /** The timeout. The alert is not shown if this timeout is 0. */
131: private static final long TIMEOUT = Configuration.getIntProperty(
132: "suspendAlertTime", 0);
133:
134: /**
135: * Determines whether the suspend alert should be shown at all,
136: * zero timeout means that it should not.
137: * @return true if timeout is configured for showing the alert,
138: * false if the alert should not be shown.
139: */
140: static boolean shouldShow() {
141: return 0 != TIMEOUT;
142: }
143:
144: /**
145: * Starts the timer that prevents system from suspension. When timeout
146: * expires, the timer will not prevent from suspension any more.
147: */
148: static void start() {
149: if (0 == TIMEOUT) {
150: return;
151: }
152:
153: final AlertTimer timer = new AlertTimer();
154: SuspendSystem.getInstance().addSuspendDependency(timer);
155:
156: TimerTask removeDep = new TimerTask() {
157: public void run() {
158: SuspendSystem.getInstance().removeSuspendDependency(
159: timer);
160: }
161: };
162:
163: timer.schedule(removeDep, TIMEOUT);
164: }
165: }
|