Show Configs.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* ConfigForm.java
* Created on Jan 27, 2004
*/
package fi.hip.gb.client;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.bluetooth.BluetoothStateException;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import fi.hip.gb.bluetooth.BTService;
import fi.hip.gb.midlet.util.StorageUtils;
/**
* Form for changing program settings and helper methods for
* accessing record store.
*
* @author Juho Karppinen
*/
public class Configs extends Form implements CommandListener {
/** stores the state of loading */
private boolean loadFailed = false;
/** protocol, either http or https */
private ChoiceGroup cgHostProtocol;
/** host name of ip address */
private TextField tfHostName;
/** port number of server */
private TextField tfHostPort;
/** completer URL of jar file containing agents */
private TextField tfJar;
/** update rate */
private ChoiceGroup cgUpdate;
/** myproxy server hostname or ip address */
private TextField tfMyProxy;
/** bluetooth server enabled */
private ChoiceGroup cgBluetooth;
/** default bluetooth service */
private TextField tfBluetoothService;
/** name of record store for confiturations */
public final static String CONFIG_STORAGE = "configs";
/** name of record store for miscellanous variables */
public static final String VAR_STORAGE = "variables";
/** record store index for loginname */
public static final int INDEX_LOGINNAME = 1;
/** record store index for sessionid */
public static final int INDEX_SESSIONID = 2;
/** record store index for proxy expiration time */
public static final int INDEX_EXPIRATION = 3;
/** record store index for bluetooth service */
public static final int INDEX_BLUETOOTSERVICE = 4;
/** the default path for GBAgent service */
private final static String DEFAULT_HOSTPATH = "gb-agent/mobile/service?=";
/** available protocols */
private final static String[] PROTOCOLS = {"http", "https"};
/** available refresh rates in milliseconds */
private final static int[] RATE_VALUES =
{1000, 5000, 10000, 30000, 60000, -1};
/** available refresh rate descriptions */
private final static String[] RATE_TEXTS =
{ "1 sec", "5 sec", "10 sec", "30 sec", "1 min", "manual" };
public Configs(){
super("Configurations");
readConfig();
append(cgHostProtocol);
append(tfHostName);
append(tfHostPort);
append(tfJar);
append(cgUpdate);
append(tfMyProxy);
// append(cgBluetooth);
append(tfBluetoothService);
addCommand(new Command("Save", Command.OK, 1));
addCommand(new Command("Back", Command.BACK, 2));
setCommandListener(this);
}
/**
* Gets the status of loading
* @return true if loading failed, false if loading
* succeeded
*/
public boolean loadFailed() {
return loadFailed;
}
/**
* Gets the name of GBAgent server
* @return hostname
*/
public String getServerName() {
return tfHostName.getString()
+ ":"
+ tfHostPort.getString();
}
/**
* Gets the full URL of GBAgent server
* @return full URL address
*/
public String getServerURL() {
return (cgHostProtocol.getSelectedIndex()==0 ? "http" : "https")
+ "://"
+ tfHostName.getString() + ":" + tfHostPort.getString()
+ "/" + DEFAULT_HOSTPATH;
}
/**
* Gets URL of the JAR file
* @return full URL address
*/
public String getJarURL() {
return tfJar.getString();
}
/**
* Gets interval time between status updates
* from server
* @return time in milliseconds, or -1 if automatic
* updation should be disabled
*/
public int getUpdateInterval() {
int index = cgUpdate.getSelectedIndex();
return RATE_VALUES[index];
}
/**
* Gets address of myproxy server
* @return ip-number or hostname
*/
public String getMyProxyServer() {
return tfMyProxy.getString();
}
/**
* Gets the bluetooth server enabled status
* @return true if bluetooth server should be enabled
*/
public boolean getBluetoothServer() {
return cgBluetooth.getSelectedIndex() == 1;
}
/**
* Gets the URL to the default Bluetooth service
* @return bluetooth service url
*/
public String getBluetoothService() {
return tfBluetoothService.getString();
}
/**
* Read configurations which are set through
* config form.
*/
private void readConfig() {
cgHostProtocol =
new ChoiceGroup(
"Host protocol",
Choice.EXCLUSIVE,
PROTOCOLS,
null);
if (MIDui.instance.getAppProperty("HostProtocol") != null) {
cgHostProtocol.setSelectedIndex(
Integer.parseInt(MIDui.instance.getAppProperty("HostProtocol")),
true);
}
tfHostName = new TextField("Host name: ",
MIDui.instance.getAppProperty("HostName"),
64,
TextField.URL);
tfHostPort = new TextField("Host port: ",
MIDui.instance.getAppProperty("HostPort"),
5,
TextField.NUMERIC);
tfJar = new TextField("Jar URL: ",
MIDui.instance.getAppProperty("JarURL"),
64,
TextField.URL);
cgUpdate = new ChoiceGroup(
"Update status interval",
Choice.POPUP,
RATE_TEXTS,
null);
if (MIDui.instance.getAppProperty("Update") != null)
cgUpdate.setSelectedIndex(
Integer.parseInt(MIDui.instance.getAppProperty("Update")),
true);
else
cgUpdate.setSelectedIndex(cgUpdate.size()-1, true);
tfMyProxy =
new TextField(
"Myproxy server: ",
MIDui.instance.getAppProperty("MyProxyHost"),
64,
TextField.ANY);
cgBluetooth =
new ChoiceGroup(
"Bluetooth server (requires restart)",
Choice.EXCLUSIVE,
new String[] {"No", "Yes"},
null);
tfBluetoothService = new TextField("Default BT: ",
MIDui.instance.getAppProperty("DefaultBluetoothService"),
64,
TextField.URL);
try {
DataInputStream dis = StorageUtils.read(CONFIG_STORAGE, 1);
cgHostProtocol.setSelectedIndex(dis.readInt(), true);
tfHostName.setString(dis.readUTF());
tfHostPort.setString(dis.readUTF());
tfJar.setString(dis.readUTF());
cgUpdate.setSelectedIndex(dis.readInt(), true);
tfMyProxy.setString(dis.readUTF());
cgBluetooth.setSelectedIndex(dis.readInt(), true);
//tfBluetoothService.setString(dis.readUTF());
System.out.println(
"Read config: "
+ " protocol="
+ cgHostProtocol.getString(cgHostProtocol.getSelectedIndex())
+ " host="
+ tfHostName.getString()
+ " port="
+ tfHostPort.getString()
+ " jarfile="
+ tfJar.getString()
+ " update="
+ cgUpdate.getString(cgUpdate.getSelectedIndex())
+ " myproxy="
+ tfMyProxy.getString()
+ " bluetooth server="
+ cgBluetooth.getString(cgBluetooth.getSelectedIndex())
+ " default bluetooth service " + tfBluetoothService.getString());
String defaultBT = Configs.readString(Configs.INDEX_BLUETOOTSERVICE);
if(defaultBT != null && defaultBT.length() > 0)
tfBluetoothService.setString(defaultBT);
} catch (Exception e) {
MIDui.log("Failed to load config: " + e.getMessage());
loadFailed = true;
}
}
/**
* Store the contigurations which are set through
* config form.
*/
private void storeConfig() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(cgHostProtocol.getSelectedIndex());
dos.writeUTF(tfHostName.getString());
dos.writeUTF(tfHostPort.getString());
dos.writeUTF(tfJar.getString());
dos.writeInt(cgUpdate.getSelectedIndex());
dos.writeUTF(tfMyProxy.getString());
dos.writeInt(cgBluetooth.getSelectedIndex());
//dos.writeUTF(tfBluetoothService.getString());
dos.close();
StorageUtils.store(baos.toByteArray(), CONFIG_STORAGE, 1);
System.out.println(
"Saved config: "
+ " protocol=" + PROTOCOLS[cgHostProtocol.getSelectedIndex()]
+ " host=" + tfHostName.getString()
+ " port=" + tfHostPort.getString()
+ " jar=" + tfJar.getString()
+ " update=" + RATE_TEXTS[cgUpdate.getSelectedIndex()]
+ " myproxy=" + tfMyProxy.getString()
+ " bluetooth=" + cgBluetooth.getString(cgBluetooth.getSelectedIndex())
+ " btservice=" + tfBluetoothService.getString());
Configs.storeString(Configs.INDEX_BLUETOOTSERVICE, tfBluetoothService.getString());
} catch (Exception e) {
MIDui.showException("Failed to store config", e);
}
}
public void commandAction(Command c, Displayable disp) {
if ((c.getCommandType() == Command.BACK)) {
readConfig();
} else if (c.getCommandType() == Command.OK) {
storeConfig();
// change new url
MIDui.getClient().setServiceURL(getServerURL(), tfMyProxy.getString());
// bluetooth enabling/disabling
if(BTService.getInstance() != null) {
if(BTService.getInstance().isListening()
&& cgBluetooth.getSelectedIndex() == 0) {
BTService.getInstance().stopServer();
} else if(cgBluetooth.getSelectedIndex() == 1) {
try {
BTService.getInstance().startServer();
} catch (BluetoothStateException e) {
e.printStackTrace();
}
}
}
}
MIDui.showMainForm();
}
/**
* Reads invidual record from the record store
* @param recordID ID of the record
* @return String prensentation of the record, or null if not
* found or readable
*/
public static String readString(int recordID) {
try {
DataInputStream dis = StorageUtils.read(VAR_STORAGE, recordID);
String record = dis.readUTF();
dis.close();
System.out.println("Read record: " + recordID + " = " + record);
return record;
} catch (Exception e) {
System.out.println("Failed to read record " + recordID
+ " : " + e.getMessage());
return null;
}
}
/**
* Writes invidual record into the record store
* @param recordID ID of the record
* @param value value of the record
*/
public static void storeString(int recordID, String value) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeUTF(value);
dos.close();
StorageUtils.store(baos.toByteArray(), VAR_STORAGE, recordID);
System.out.println("Saved record " + recordID + " = "+ value);
} catch (Exception e) {
System.out.println("Failed to store record " + recordID
+ " : " + e.getMessage());
}
}
/**
* Reads invidual long record from the record store
* @param recordID ID of the record
* @return long value, or -1 if record is not found
* or readable
*/
public static long readLong(int recordID) {
try {
DataInputStream dis = StorageUtils.read(VAR_STORAGE, recordID);
long record = dis.readLong();
dis.close();
System.out.println("Read record: " + recordID + " = " + record);
return record;
} catch (Exception e) {
System.out.println("Failed to read record " + recordID
+ " : " + e.getMessage());
return -1;
}
}
/**
* Writes invidual long record into the record store
* @param recordID ID of the record
* @param value value of the record
*/
public static void storeLong(int recordID, long value) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeLong(value);
dos.close();
StorageUtils.store(baos.toByteArray(), VAR_STORAGE, recordID);
System.out.println("Saved record " + recordID + " = "+ value);
} catch (Exception e) {
System.out.println("Failed to store record " + recordID
+ " : " + e.getMessage());
}
}
}
See more files for this project here