midletpeer.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 499 行 · 第 1/2 页

JAVA
499
字号
/* * * * 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.midlet;import javax.microedition.io.ConnectionNotFoundException;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import com.sun.midp.security.Permissions;import com.sun.midp.security.SecurityToken;/** * MIDletPeer maintains the current state of the MIDlet and forwards updates * to it.  It contains a reference to the MIDlet itself. * Control methods (startApp, destroyApp, * pauseApp) defined here are invoked on the MIDlet object via the * MIDletTunnel. * <p> * All state changes are synchronized using midletStateHandler retrieved * from the MIDletStateHandler. * NotifyPaused, ResumeRequest, and NotifyDestroyed methods invoked on the * MIDlet cause the appropriate state change.  The MIDletStateHandler is aware * of changes by waiting on the midletStateHandler. */public class MIDletPeer implements MIDletEventConsumer {    /*     * Implementation state; the states are in priority order.     * That is, a higher number indicates a preference to be     * selected for activating sooner.  This allows the MIDlet state handler     * to make one pass over the known MIDlets and pick the     * "best" MIDlet to activate.     */    /**     * State of the MIDlet is Paused; it should be quiescent     */    public static final int PAUSED = 0;    /**     * State of the MIDlet is Active     */    public static final int ACTIVE = 1;    /**     * State of the MIDlet when resumed by the AMS     */    static final int ACTIVE_PENDING = 2;    /**     * State of the MIDlet when paused by the AMS     */    static final int PAUSE_PENDING = 3;    /**     * State of the MIDlet with destroy pending     */    static final int DESTROY_PENDING = 4;    /**     * State of the MIDlet is Destroyed     */    public static final int DESTROYED = 5;    /** The controller of MIDlets. */    private static MIDletStateHandler midletStateHandler;    /** The call when a MIDlet's state changes. */    private static MIDletStateListener midletStateListener;    /** Handles platform requests. */    private static PlatformRequest platformRequest;    /** The MIDletTunnel implementation from javax.microedition.midlet */    private static MIDletTunnel tunnel;    /**     * Initialize the MIDletPeer class. Should only be called by the     * MIDletPeerList (MIDletStateHandler).     *     * @param theMIDletStateHandler the midlet state handler     * @param theMIDletStateListener the midlet state listener     * @param thePlatformRequestHandler the platform request handler     */    static void initClass(        MIDletStateHandler theMIDletStateHandler,        MIDletStateListener theMIDletStateListener,        PlatformRequest thePlatformRequestHandler) {        midletStateHandler = theMIDletStateHandler;        midletStateListener = theMIDletStateListener;        platformRequest = thePlatformRequestHandler;    }    /**     * Sets up the reference to the MIDletTunnel implementation.     * This must be called exactly once during system initialization.     *     * @param token security token for authorizing the caller     * @param t the MIDletTunnel implementation     */    public static void setMIDletTunnel(SecurityToken token, MIDletTunnel t) {        token.checkIfPermissionAllowed(Permissions.MIDP);        tunnel = t;    }    /**     * Returns the MIDletPeer object corresponding to the given     * midlet instance.     *     * @param m the midlet instance     *     * @return MIDletPeer instance associate with m     */    static MIDletPeer getMIDletPeer(MIDlet m) {        return tunnel.getMIDletPeer(m);    }    /**     * The applications current state.     */    private int state;    /**     * The MIDlet for which this is the state.     */    protected MIDlet midlet;    /**     * Creates a MIDlet's peer which is registered the MIDletStateHandler.     * Shall be called only from MIDletStateHandler.     * <p>     * The peer MIDlet field is set later when the MIDlet's constructor calls     * newMidletState.     */    MIDletPeer() {        state = ACTIVE_PENDING;        // So it will be made active soon    }    /**     * Get the MIDlet for which this holds the state.     *     * @return the MIDlet; will not be null.     */    public MIDlet getMIDlet() {        return midlet;    }    /**     * Forwards startApp to the MIDlet.     *     * @exception <code>MIDletStateChangeException</code>  is thrown if the     *                <code>MIDlet</code> cannot start now but might be able     *                to start at a later time.     */    void startApp() throws MIDletStateChangeException {        tunnel.callStartApp(midlet);    }    /**     * Forwards pauseApp to the MIDlet.     *     */    void pauseApp() {        tunnel.callPauseApp(midlet);    }    /**     * Forwards destoryApp to the MIDlet.     *     * @param unconditional the flag to pass to destroy     *     * @exception <code>MIDletStateChangeException</code> is thrown     *                if the <code>MIDlet</code>     *          wishes to continue to execute (Not enter the <i>Destroyed</i>     *          state).     *          This exception is ignored if <code>unconditional</code>     *          is equal to <code>true</code>.     */    void destroyApp(boolean unconditional)        throws MIDletStateChangeException {        tunnel.callDestroyApp(midlet, unconditional);    }    /**     *     * Used by a <code>MIDlet</code> to notify the application management     * software that it has entered into the     * <i>DESTROYED</i> state.  The application management software will not     * call the MIDlet's <code>destroyApp</code> method, and all resources     * held by the <code>MIDlet</code> will be considered eligible for     * reclamation.     * The <code>MIDlet</code> must have performed the same operations     * (clean up, releasing of resources etc.) it would have if the     * <code>MIDlet.destroyApp()</code> had been called.     *     */    public final void notifyDestroyed() {        synchronized (midletStateHandler) {            state = DESTROYED;            midletStateHandler.notify();        }    }    /**     * Used by a <code>MIDlet</code> to notify the application management     * software that it has entered into the <i>PAUSED</i> state.     * Invoking this method will     * have no effect if the <code>MIDlet</code> is destroyed,     * or if it has not yet been started. <p>     * It may be invoked by the <code>MIDlet</code> when it is in the     * <i>ACTIVE</i> state. <p>     *     * If a <code>MIDlet</code> calls <code>notifyPaused()</code>, in the     * future its <code>startApp()</code> method may be called make     * it active again, or its <code>destroyApp()</code> method may be     * called to request it to destroy itself.     */    public final void notifyPaused() {        int oldState;        synchronized (midletStateHandler) {            oldState = state;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?