📄 connectionscreen.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.client.ui;import java.io.IOException;import java.util.Vector;import javax.bluetooth.DeviceClass;import javax.bluetooth.RemoteDevice;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Choice;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.List;import fildiv.jremcntl.client.core.AppConstants;import fildiv.jremcntl.client.core.AppContext;import fildiv.jremcntl.client.util.UIUtils;import fildiv.jremcntl.common.core.JRemClientConnectionListener;import fildiv.jremcntl.common.core.JRemClientConnector;import fildiv.jremcntl.common.util.JRemUtils;public class ConnectionScreen extends AbstractScreen implements CommandListener { private final static Command CMD_BACK = new Command("Back", Command.BACK, 1); private final static Command CMD_SCAN = new Command("Scan", Command.SCREEN, 2); private final static Command CMD_STOP = new Command("Stop", Command.STOP, 1); private static final String DEFAULT_INITIAL_MESSAGE = "Select scan button to find a bluetooth device"; private JRemClientConnector connector; private List deviceList = null; private Vector devices; private Form connForm; private JRemClientConnectionListener connListener = new JRemClientConnectionListener() { public synchronized void discoveryCompleted(Vector devices, boolean error) { ConnectionScreen.this.devices = devices; onDiscoveryCompleted(error); } public void inProgress() { } public synchronized void discoveryStarted() { onDiscoveryStarted(); } public void connected(boolean success) { onConnected(success); } public void interrupt() { onInterrupt(); } public void exceptionOccurred(Exception e) { onExceptionOccurred(e); } public void serviceOpening() { onServiceOpening(); } public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) { onDeviceDiscovered(rd); } }; private String url; public ConnectionScreen(AppContext appCtx, Screen backScreen) { super(appCtx, backScreen); this.connector = appCtx.getConnector(); this.connector.setConnectorListener(connListener); connForm = new Form(AppConstants.CONNECTION_SCREEN_CAPTION); init(false); } public void showAndConnect(String url) { this.url = url; show(); } protected void init(boolean showCommands) { connForm.deleteAll(); connForm.setCommandListener(this); if (showCommands) setupCommands(false); else removeAllCommands(); } protected void onActivate() { reset(false, true, false); if (!JRemUtils.isEmptyString(url)) connectToURL(); else findDevices(); } private void reset(boolean initialMessage, boolean initialDisplayable, boolean showCommands) { stopWait(); deviceList = null; init(showCommands); if (initialDisplayable) getDisplay().setCurrent(connForm); if (initialMessage) UIUtils.appendText(connForm, DEFAULT_INITIAL_MESSAGE); } private void setupCommands(boolean inBTOperation) { removeAllCommands(); if (inBTOperation) connForm.addCommand(CMD_STOP); else { connForm.addCommand(CMD_BACK); connForm.addCommand(CMD_SCAN); } } private void removeAllCommands() { connForm.removeCommand(CMD_BACK); connForm.removeCommand(CMD_SCAN); connForm.removeCommand(CMD_STOP); } public void commandAction(Command c, Displayable disp) { if (c == CMD_BACK) { if (deviceList != null) reset(true, true, true); else hide(); } else { if (disp == connForm) { if (c == CMD_SCAN) findDevices(); else if (c == CMD_STOP) connector.interrupt(); } else { // Device selected selectDevice((List) disp); deviceList = null; } } } private synchronized void findDevices() { connector.findDevices(); } private synchronized void connectToURL() { try { showWait(connForm, "Opening ..."); connector.connect(url); } finally { this.url = null; } } private void selectDevice(List list) { RemoteDevice rd = (RemoteDevice) devices.elementAt(list.getSelectedIndex()); // Save last device url AppContext appCtx = getAppCtx(); appCtx.setLastDeviceAddr(rd.getBluetoothAddress()); connector.connect(rd); } private void showDeviceSelector() { String devicesItems[] = new String[devices.size()]; for (int index = 0; index < devices.size(); ++index) { RemoteDevice rd = (RemoteDevice) devices.elementAt(index); devicesItems[index] = getDeviceName(rd); } deviceList = new List("Device list", Choice.IMPLICIT, devicesItems, null); deviceList.addCommand(CMD_BACK); deviceList.setCommandListener(this); getDisplay().setCurrent(deviceList); } private String getDeviceName(RemoteDevice rd) { String fn = ""; try { fn = rd.getFriendlyName(false); } catch (IOException e) { fn = rd.getBluetoothAddress(); } return fn; } /* * Connector events */ private void onDiscoveryStarted() { deviceList = null; connForm.deleteAll(); setupCommands(true); showWait(connForm, "Scanning ..."); } protected void onDiscoveryCompleted(boolean error) { stopWait(); connForm.deleteAll(); if (error) { UIUtils.showAlert(getDisplay(), connForm, "An error has occurred during scan operation!", AlertType.ERROR); } if (devices.size() > 0) showDeviceSelector(); else { UIUtils.showAlert(getDisplay(), connForm, "Unable to find a device!", AlertType.INFO); reset(true, false, true); } } protected void onServiceOpening() { connForm.deleteAll(); setupCommands(true); showWait(connForm, "Opening ..."); } protected void onConnected(boolean success) { stopWait(); AppContext appCtx = getAppCtx(); if (success) { appCtx.setLastURL(appCtx.getConnector().getURL()); hide(); } else { UIUtils.showAlert(getDisplay(), connForm, "Unable to connect to device!", AlertType.INFO); reset(true, false, true); } } private void onInterrupt() { UIUtils.showAlert(getDisplay(), connForm, "Action interrupted!", AlertType.INFO); } protected void onDeviceDiscovered(RemoteDevice rd) { String deviceName = getDeviceName(rd); connForm.append(deviceName); connForm.append("\n"); } private void onExceptionOccurred(Exception e) { reset(true, false, true); UIUtils.showAlert(getDisplay(), connForm, "An error has occurred during operation", AlertType.ERROR); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -