mvmmanager.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 677 行 · 第 1/2 页
JAVA
677 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.appmanager;import java.util.Enumeration;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import com.sun.midp.i18n.*;import com.sun.midp.midlet.*;import com.sun.midp.installer.*;import com.sun.midp.main.*;import com.sun.midp.configurator.Constants;import com.sun.midp.events.EventQueue;import com.sun.midp.events.NativeEvent;import com.sun.midp.events.EventTypes;/** * This is an implementation of the ApplicationManager interface * for the MVM mode of the VM capable of running with * more than 1 midlet concurrently. * * Application manager controls midlet life cycle: * - installs, updates and removes midlets/midlet suites * - launches, moves to foreground and terminates midlets * - displays info about a midlet/midlet suite * - shuts down the AMS system */public class MVMManager extends MIDlet implements MIDletProxyListListener, DisplayControllerListener, ApplicationManager, ODTControllerEventConsumer, AMSServicesEventConsumer, InstallerEventConsumer { /** Constant for the discovery application class name. */ private static final String DISCOVERY_APP = "com.sun.midp.installer.DiscoveryApp"; /** Constant for the graphical installer class name. */ private static final String INSTALLER = "com.sun.midp.installer.GraphicalInstaller"; /** Constant for the CA manager class name. */ private static final String CA_MANAGER = "com.sun.midp.appmanager.CaManager"; /** Constant for the Component manager class name. */ private static final String COMP_MANAGER = "com.sun.midp.appmanager.ComponentManager"; /** Constant for the ODT Agent class name. */ private static final String ODT_AGENT = "com.sun.midp.odd.ODTAgentMIDlet"; /** True until constructed for the first time. */ private static boolean first = true; /** Screen that displays all installed midlets and installer */ private AppManagerPeer appManager; /** MIDlet proxy list reference. */ private MIDletProxyList midletProxyList; /** UI to display error alerts. */ private DisplayError displayError; /** If on device debug is active, ID of the suite under debug. */ private int suiteUnderDebugId = MIDletSuite.UNUSED_SUITE_ID; /** A thread waiting until a midlet is terminated. Can be null. */ private Thread waitForDestroyThread; /** A synchronization object. */ private static final Object waitForDestroy = new Object(); /** * Create and initialize a new MVMManager MIDlet. */ public MVMManager() { MIDletProxy thisMidlet; EventQueue eq = EventQueue.getEventQueue(); new ODTControllerEventListener(eq, this); new AMSServicesEventListener(eq, this); new InstallerEventListener(eq, this); midletProxyList = MIDletProxyList.getMIDletProxyList(); // The proxy for this MIDlet may not have been create yet. for (; ; ) { thisMidlet = midletProxyList.findMIDletProxy( MIDletSuite.INTERNAL_SUITE_ID, this.getClass().getName()); if (thisMidlet != null) { break; } try { Thread.sleep(10); } catch (InterruptedException ie) { // ignore } } MVMDisplayController dc = new MVMDisplayController( midletProxyList, thisMidlet); midletProxyList.setDisplayController(dc); dc.addListener(this); IndicatorManager.init(midletProxyList); GraphicalInstaller.initSettings(); first = (getAppProperty("logo-displayed") == null); Display display = Display.getDisplay(this); displayError = new DisplayError(display); // AppManagerUI will be set to be current at the end of its constructor appManager = new AppManagerPeer(this, display, displayError, first, null); /* * Listen to the MIDlet proxy list. * This allows us to notify the Application Selector * of any changes whenever switch back to the AMS. * The listener must be set up after finishing all * initialization in the constructor. */ midletProxyList.addListener(this); processArguments(); if (first) { first = false; } } /** * Processes MIDP_ENABLE_ODD_EVENT */ public void handleEnableODDEvent() { appManager.showODTAgent(); } /** * Processes MIDP_ODD_START_MIDLET_EVENT * * @param suiteId ID of the midlet suite * @param className class name of the midlet to run * @param displayName display name of the midlet to run * @param isDebugMode true if the midlet must be started in debug mode, * false otherwise */ public void handleODDStartMidletEvent(int suiteId, String className, String displayName, boolean isDebugMode) { /* For the case of showing MIDlet selector, we need AMS to have * foreground. */ MIDletProxy thisMidlet = midletProxyList.findMIDletProxy( MIDletSuite.INTERNAL_SUITE_ID, this.getClass().getName()); midletProxyList.setForegroundMIDlet(thisMidlet); if (suiteUnderDebugId != MIDletSuite.UNUSED_SUITE_ID) { /* IMPL NOTE: this forces only one running MIDlet in debug mode - * the VM currently does not support more MIDlets in debug mode * at the same time. */ isDebugMode = false; } try { appManager.launchSuite(suiteId, className, isDebugMode, true); if (isDebugMode) { suiteUnderDebugId = suiteId; } } catch (Exception ex) { displayError.showErrorAlert(displayName, ex, null, null); } } /** * Processes MIDP_ODD_EXIT_MIDLET_EVENT. * * @param suiteId ID of the midlet suite * @param className class name of the midlet to exit or <code>NULL</code> * if all MIDlets from the suite should be exited */ public void handleODDExitMidletEvent(final int suiteId, final String className) { appManager.exitSuite(suiteId, className); } /** * Processes MIDP_ODD_SUITE_INSTALLED_EVENT. This event indicates that * a new MIDlet suite has been installed by ODT agent. It is signal for * the application manager to update the displayed list of MIDlets. * * @param suiteId ID of the newly installed MIDlet suite */ public void handleODDSuiteInstalledEvent(int suiteId) { appManager.notifySuiteInstalled(suiteId); } /** * Processes MIDP_ODD_SUITE_REMOVED_EVENT. This event indicates that * an installed MIDlet suite has been removed by ODT agent. It is signal for * the application manager to update the displayed list of MIDlets. * * @param suiteId ID of the removed MIDlet suite */ public void handleODDSuiteRemovedEvent(int suiteId) { appManager.notifySuiteRemoved(suiteId); } /** * Processes MIDP_KILL_MIDLETS_EVENT. * * @param suiteId ID of the midlet suite from which the midlets * must be killed * @param isolateId ID of the isolate requested this operation */ public void handleKillMIDletsEvent(int suiteId, int isolateId) { try { int timeout = 1000 * Configuration.getIntProperty( "destoryMIDletTimeout", 5); Enumeration proxies = midletProxyList.getMIDlets(); while (proxies.hasMoreElements()) { MIDletProxy mp = (MIDletProxy)proxies.nextElement(); if (mp.getSuiteId() != suiteId) { continue; } // kill the running midlet and wait until it is destroyed mp.destroyMidlet(); try { synchronized(waitForDestroy) { waitForDestroy.wait(timeout); } } catch (InterruptedException ie) { // ignore } } } catch (Exception ex) { displayError.showErrorAlert(null, ex, null, null); } finally { // notify the listeners that the midlets were killed NativeEvent event = new NativeEvent( EventTypes.MIDP_MIDLETS_KILLED_EVENT); event.intParam1 = suiteId; EventQueue eq = EventQueue.getEventQueue(); eq.sendNativeEventToIsolate(event, isolateId); } } /** * Processes RESTART_MIDLET_EVENT. * * @param externalAppId application ID assigned by externall App Manager; * not used if there is no external manager * @param suiteId ID of the midlet suite * @param className class name of the midlet to restart * @param displayName display name of the midlet to restart */ public void handleRestartMIDletEvent(int externalAppId, int suiteId, String className, String displayName) { try { final MIDletProxy mp = midletProxyList.findMIDletProxy(suiteId, className); if (mp != null) { final int id = suiteId; final String nameOfClass = className; final String nameOfMIDlet = displayName; // wait until the midlet is destroyed waitForDestroyThread = new Thread() { public void run() { try { int timeout = 1000 * Configuration.getIntProperty( "destoryMIDletTimeout", 5); synchronized(waitForDestroyThread) { wait(timeout); } } catch(InterruptedException ie) { // ignore } MIDletSuiteUtils.execute(id, nameOfClass, nameOfMIDlet); } }; waitForDestroyThread.start(); mp.destroyMidlet(); } else { // the midlet is not running, just starting it MIDletSuiteUtils.execute(suiteId, className, displayName); } } catch (Exception ex) { displayError.showErrorAlert(displayName, ex, null, null); } } // =================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?