FileDocCategorySizeDatePackage
DisplayController.javaAPI DocphoneME MR2 API (J2ME)15153Wed May 02 18:00:06 BST 2007com.sun.midp.main

DisplayController

public class DisplayController extends Object
This class controls which MIDlet's display is in the foreground. Running only in the AMS Isolate (0) the controller consulted by the MIDlet proxy list for any foreground when various state changes occur in a MIDlet. The display controller automatically selects the next foreground if needed.

From the user perspective when the last MIDlet the user launched sets its current displayable for the first time, that MIDlet should automatically get the foreground (see the midletCreated and foregroundRequest methods).

A MIDlet that is paused or destroyed is treated as if it has requested the background as described above.

Fields Summary
protected MIDletProxy
lastMidletCreated
The last MIDlet added to the MIDlet proxy list, but has not requested to be in the foreground.
protected MIDletProxyList
midletProxyList
Cache of the MIDlet proxy list reference.
private Vector
listeners
What objects want to listen.
Constructors Summary
protected DisplayController(MIDletProxyList theMIDletProxyList)
Construct a DisplayController with a reference to the ProxyList.

param
theMIDletProxyList reference to the MIDlet proxy list


                          
       
        midletProxyList = theMIDletProxyList;
    
Methods Summary
public voidaddListener(DisplayControllerListener listener)
Add a listener.

param
listener DisplayController listener

        listeners.addElement(listener);
    
MIDletProxybackgroundRequest(MIDletProxy midlet)
Handles MIDlet background requests.

If the MIDlet is requesting to be put in the background is the foreground MIDlet, then find a MIDlet to bring to the foreground (see the findNextForeground method).

param
midlet The proxy of the MIDlet that was updated
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        MIDletProxy foreground = midletProxyList.getForegroundMIDlet();

        if (midlet != foreground) {
            // not in the foreground, so don't change the foreground
            return foreground;
        }

        return findNextForegroundMIDlet();
    
private voidclearLastMidletCreated(MIDletProxy midlet)
Clear the last MIDlet created, if it is one given.

param
midlet a proxy of the MIDlet that may be the last MIDlet created

        if (lastMidletCreated == midlet) {
            lastMidletCreated = null;
        }
    
MIDletProxyendPreempting(int isolateId, int displayId)
End the preempt an Isolate's displays.

param
isolateId isolate ID of display that done preempting
param
displayId display ID of display that done preempting
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        Enumeration midlets;
        MIDletProxy foreground;
        MIDletProxy preempted;
        MIDletProxy preempting = null;

        /*
         * Stop preempting all of the MIDlets that were preempted.
         */
        midlets = midletProxyList.getMIDlets();
        while (midlets.hasMoreElements()) {
            MIDletProxy current = (MIDletProxy)midlets.nextElement();
            MIDletProxy temp;

            temp = current.getPreemptingDisplay();
            if (temp == null) {
                continue;
            }

            if (temp.getIsolateId() != isolateId) {
                continue;
            }

            if (temp.getDisplayId() != displayId) {
                continue;
            }

            preempting = temp;

            current.setPreemptingDisplay(null);
            midletProxyList.notifyListenersOfProxyUpdate(current, 
                MIDletProxyListListener.PREEMPTING_DISPLAY);
        }

        foreground = midletProxyList.getForegroundMIDlet();
        if (foreground == null) {
            return null;
        }

        if (preempting == null) {
            return foreground;
        }

        preempted = preempting.getPreemptedMidlet();

        // if the preempting display is not in the foreground then do nothing
        if (foreground != preempting) {
            return foreground;
        }

        preempted = preempting.getPreemptedMidlet(); 

        if (preempted != null) {
            return preempted;
        }

        return foreground;
    
private MIDletProxyfindNextForegroundMIDlet()
Find a MIDlet that wants the foreground. If none wants the foreground then find one that is not paused, if no find one that is paused and wants the foreground, then find one that is paused.

return
new foreground task or null

        Enumeration midlets;

        // find the first task that is active and wants foreground
        midlets = midletProxyList.getMIDlets();
        while (midlets.hasMoreElements()) {
            MIDletProxy current = (MIDletProxy)midlets.nextElement();

            if (current.getMidletState() != MIDletProxy.MIDLET_ACTIVE) {
                continue;
            }

            if (current.wantsForeground()) {
                return current;
            }
        }

        // find the first task that is active
        midlets = midletProxyList.getMIDlets();
        while (midlets.hasMoreElements()) {
            MIDletProxy current = (MIDletProxy)midlets.nextElement();

            if (current.getMidletState() != MIDletProxy.MIDLET_ACTIVE) {
                return current;
            }
        }

        // find the first task that is paused and wants the foreground
        midlets = midletProxyList.getMIDlets();
        while (midlets.hasMoreElements()) {
            MIDletProxy current = (MIDletProxy)midlets.nextElement();

            if (current.getMidletState() != MIDletProxy.MIDLET_PAUSED) {
                continue;
            }

            if (current.wantsForeground()) {
                return current;
            }
        }

        // find the first task that is paused
        midlets = midletProxyList.getMIDlets();
        while (midlets.hasMoreElements()) {
            MIDletProxy current = (MIDletProxy)midlets.nextElement();

            if (current.getMidletState() != MIDletProxy.MIDLET_PAUSED) {
                return current;
            }
        }

        return null;
    
MIDletProxyforegroundMidletChanging(MIDletProxy midlet)
Call to notify that foreground MIDlet is changing and give the display controller a chance to preempt the change. Also the last MIDlet created state will be reset.

If the MIDlet to get the foreground is paused, then activate it.

param
midlet proxy of the MIDlet to be put in the foreground
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        MIDletProxy preempting;

        if (midlet == null) {
            return null;
        }

        preempting = midlet.getPreemptingDisplay();
        if (preempting != null) {
            return preempting;
        }

        clearLastMidletCreated(midlet);

        if (midlet.getMidletState() == MIDletProxy.MIDLET_PAUSED) {
            midlet.activateMidlet();
        }

        return midlet;
    
MIDletProxyforegroundRequest(MIDletProxy midlet)
Handles MIDlet foreground requests.

If proxy being updated belongs last MIDlet created in the proxy list, then put the MIDlet in the foreground.

If there is no foreground MIDlet or the foreground MIDlet does not want the foreground or the foreground MIDlet is paused, then put the MIDlet in the foreground.

param
midlet The proxy of the MIDlet that was updated
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        MIDletProxy foreground;

        /*
         * When the last MIDlet started wants the foreground automatically
         * put in the foreground this time only.
         */
        if (midlet == lastMidletCreated) {
            return midlet;
        }

        foreground = midletProxyList.getForegroundMIDlet();
        if (foreground == null || !foreground.wantsForeground() ||
            foreground.getMidletState() == MIDletProxy.MIDLET_PAUSED) {
            return midlet;
        }

        // don't change the foreground
        return foreground;
    
MIDletProxymidletActive(MIDletProxy midlet)
Called when a MIDlet is move to active state.

param
midlet The proxy of the MIDlet being activated
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        return midletProxyList.getForegroundMIDlet();
    
voidmidletCreated(MIDletProxy midlet)
Update the last MIDlet created field so when the wantsForeground field of the MIDletProxy is updated to be true, the MIDlet can be automatically put into the foreground.

Called when a MIDlet is created to the proxy list.

param
midlet The proxy of the MIDlet being created

        lastMidletCreated = midlet;
    
MIDletProxymidletDestroyed(MIDletProxy midlet)
If the removed MIDlet is the foreground MIDlet find a new foreground MIDlet. After clearing the last midlet created, treat this state change as background request.

Called when a MIDlet is removed from the proxy list.

param
midlet The proxy of the removed MIDlet
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        clearLastMidletCreated(midlet);

        return backgroundRequest(midlet);
    
MIDletProxymidletPaused(MIDletProxy midlet)
Handles any possible foreground changes due the state of a MIDlet changing to paused.

Treat this state change as a background request.

Called when the state of a MIDlet in the MIDlet proxy list is paused.

param
midlet The proxy of the MIDlet that was updated
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        return backgroundRequest(midlet);
    
voidnotifyListenersOfSelectForeground(boolean onlyFromLaunchedList)
Notify the listeners of the display controller that foreground selection ui should be launched.

param
onlyFromLaunchedList true if midlet should be selected from the list of already launched midlets, if false then possibility to launch midlet is needed.

        for (int i = listeners.size() - 1; i >= 0; i--) {
            DisplayControllerListener listener =
                (DisplayControllerListener)listeners.elementAt(i);

            listener.selectForeground(onlyFromLaunchedList);
        }
    
public voidremoveListener(DisplayControllerListener listener)
Remove a listener for DisplayController.

param
listener DisplayController listener

        listeners.removeElement(listener);
    
MIDletProxyselectForeground(boolean onlyFromLaunchedList)
Called to process a select foreground event. Processing this event only needs to be done when application MIDlets are allowed to run concurrently. In SVM mode the display controller returns foreground MIDlet.

param
onlyFromLaunchedList true if midlet should be selected from the list of already launched midlets, if false then possibility to launch midlet is needed.
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        notifyListenersOfSelectForeground(onlyFromLaunchedList);
        return midletProxyList.getForegroundMIDlet();
    
MIDletProxystartPreempting(MIDletProxy preempting)
Called to process a preempt event. The default is to preempt the foreground.

param
preempting proxy of the preempting MIDlet to be put in the foreground when a preempted MIDlet gets the foreground or to be put in the foreground directly.
return
Proxy of the next foreground MIDlet, may be the foreground MIDlet if the foreground should not change

        MIDletProxy foreground;
        
        foreground = midletProxyList.getForegroundMIDlet();
        if (foreground != null) {
            preempting.setPreemptedMidlet(foreground);
            foreground.setPreemptingDisplay(preempting);
        }

        return preempting;
    
MIDletProxytransferRequest(MIDletProxy origin, MIDletProxy target)
Request a transfer of the foreground from one MIDlet to another. The transfer only succeeds if the current foreground is the "from" MIDlet.

param
origin the MIDletProxy from which the FG should transfer from
param
target the MIDletProxy to which the FG should transfer to
return
the choice about which to become the FG

        MIDletProxy foreground = midletProxyList.getForegroundMIDlet();
        if (origin == foreground) {
            // Foreground is the requesting MIDlet, change to target
            return target;
        }

        // The foreground is not the requesting origin; don't change
        return foreground;