www.pudn.com > Bluegammon蓝牙的应用编程.rar > BluetoothDevicePage.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon.gui; 
 
 
import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.CommandListener; 
import javax.microedition.lcdui.Displayable; 
import javax.microedition.lcdui.Image; 
 
import bluegammon.Resources; 
import bluegammon.gui.menu.Menu; 
import bluegammon.gui.menu.MenuPage; 
import bluegammon.gui.menu.PageItem; 
 
/** 
 * A page that presents nearby devices, and gives 
 * feedback when search is active. Presentation and 
 * callhandler to the underlying workflow, an instance of  
 * BluetoothClientWorkflow. 
 *  
 * @see bluegammon.gui.BluetoothClientWorkflow 
 * @author Peter Andersson 
 */ 
public class BluetoothDevicePage extends MenuPage implements  
    FocusablePage, CommandListener, Runnable 
{ 
  /** The refresh command of this page */ 
  protected static final Command REFRESH = 
    new Command(Resources.getString(Resources.TXT_C_REFRESH), Command.OK, 1); 
  /** Our canvas */ 
  protected PopupCanvas m_canvas; 
  /** Our softbuttons */ 
  protected SoftButtonControl m_softButtons; 
  /** The menu */ 
  protected Menu m_menu; 
  /** Command to set when leaving page */ 
  protected Command m_oldCommand; 
  /** Listener to set when leaving page */ 
  protected CommandListener m_oldListener; 
  /** Flag indicating if we are looking for devices */ 
  protected boolean m_searching = false; 
  /** Current search title image index */ 
  protected int m_btImage = 0; 
  /** Search title images */ 
  protected Image[] m_images; 
  /** 
   * The workflow for connecting to a client that is selected from 
   * this page. 
   */ 
  protected BluetoothClientWorkflow m_clientWorkflow; 
 
  /** 
   * Creates the nearby bluetooth devices page. 
   * @param title		The title of the page. 
   * @param menu		The menu this page belongs to. 
   * @param canvas		The canvas this page is drawed upon. 
   * @param softButtons	The softbutton control used in above canvas. 
   */ 
  public BluetoothDevicePage(char[] title, Menu menu, PopupCanvas canvas, 
      SoftButtonControl softButtons) 
  { 
    super(title, null); 
    m_canvas = canvas; 
    m_softButtons = softButtons; 
    m_menu = menu; 
    m_images = new Image[2]; 
    m_images[0] = Resources.getImage(Resources.IMG_BT_1); 
    m_images[1] = Resources.getImage(Resources.IMG_BT_2); 
    new Thread(this, "BTAnim").start(); 
  } 
 
  /** 
   * Adds an item and repaints. 
   */ 
  public void addItem(PageItem item) 
  { 
    super.addItem(item); 
    m_canvas.repaint(); 
  } 
   
  /** 
   * Registers the behaviour to enable when user 
   * wants to connect as client to a device listed in 
   * this page. 
   *  
   * @param bch		The workflow for connecting to a device as client. 
   */ 
  public void setClientWorkflow(BluetoothClientWorkflow bch) 
  { 
    m_clientWorkflow = bch; 
  } 
 
  /** 
   * Returns whether this page displays the icon 
   * indicating device search or not. 
   * @return	true for searching, false otherwise. 
   */ 
  public synchronized boolean isSearching() 
  { 
    return m_searching; 
  } 
 
  /** 
   * Enables or disables the searching 
   * @param on 
   */ 
  public synchronized void setSearching(boolean on) 
  { 
    m_softButtons.enable(REFRESH, !on); 
    m_searching = on; 
    m_canvas.repaint(); 
    notifyAll(); 
  } 
   
  // FocusablePage impl 
 
  /** 
   * Called when this page shows up. Sets specific softbuttons. 
   */ 
  public void onEnter() 
  { 
    m_oldListener = m_softButtons.getCommandListener(); 
    m_oldCommand = m_softButtons.getRightCommand(); 
    m_softButtons.setCommandListener(this); 
    m_softButtons.setRightCommand(REFRESH); 
    m_softButtons.enable(REFRESH, !m_searching); 
  } 
 
  /** 
   * Called when this page is hidden for something else. 
   * Resets softbuttons. 
   */ 
  public void onLeave() 
  { 
    m_softButtons.setRightCommand(m_oldCommand); 
    m_softButtons.setCommandListener(m_oldListener); 
  } 
   
  // CommandListener impl 
 
  /** 
   * Called from softbutton control. 
   */ 
  public void commandAction(Command c, Displayable d) 
  { 
    if (c == REFRESH) 
    { 
      m_clientWorkflow.refresh(); 
    } 
    else 
    { 
      m_menu.goBack(); 
    } 
  } 
   
  // Runnable impl 
 
  /** 
   * Bluetooth search animation. 
   */ 
  public void run() 
  { 
    synchronized (this) 
    { 
      while (true) 
      { 
        while (!m_searching) 
        { 
          setTitleImage(null); 
          m_canvas.repaint(); 
          try 
          { 
            wait(); 
          } catch (InterruptedException e) { } 
        } 
        while (m_searching) 
        { 
          setTitleImage(m_images[m_btImage++]); 
          m_btImage %= m_images.length; 
          m_canvas.repaint(); 
          try 
          { 
            wait(500); 
          } catch (InterruptedException e) { } 
        } 
      } 
    } 
  } 
}