📄 .#settings.java.1.5.6.1
字号:
package no.auc.one.portableplayer;
import no.auc.one.portableplayer.communication.mediaserver.*;
import no.auc.one.portableplayer.communication.mediarenderer.*;
import no.auc.one.portableplayer.communication.*;
import no.auc.one.portableplayer.userinterface.*;
import no.auc.one.portableplayer.settings.*;
import no.auc.one.portableplayer.utils.Utilities;
import java.util.*;
import javax.microedition.rms.*;
import org.apache.log4j.*;
/**
* This class hold the settings information like if shuffle or repeat is on
* or off and which renderer that should be used.
*
* Uses singleton design pattern.
*/
public class Settings {
private static Settings ref = null;
private static Logger LOG;
private MediaServerFactory msf = new MediaServerFactory();
private MediaServer[] msList = null;
private int currentMediaServer = -1;
private boolean isUpdatingMediaServerList = false;
private MediaRendererFactory mrf = new MediaRendererFactory();
private MediaRenderer[] mrList = null;
private int currentMediaRenderer = -1;
private boolean isUpdatingMediaRendererList = false;
private boolean repeat = false;
private boolean shuffle = false;
private int hostingPort = -1;
private int manuallySelectedHostingPort = -1;
private boolean settingsChanged = false;
private static final int RECORDID_SETTING_MEDIASERVER = 1;
private static final int RECORDID_SETTING_MEDIARENDERER = 2;
private static final int RECORDID_SETTING_SHUFFLE = 3;
private static final int RECORDID_SETTING_REPEAT = 4;
private static final int RECORDID_SETTING_HOSTINGPORT = 5;
RecordStore settingsStore;
// This will consume some memory, so if that becomes a big problem then
// it could be changed to either allow only one event listener, or maybe
// use a finite array list (2 or 3 max elements).
Vector mediaRendererChangedListeners = new Vector(2);
Vector mediaServerChangedListeners = new Vector(2);
Vector playModeShuffleChangedListeners = new Vector(2);
Vector playModeRepeatChangedListeners = new Vector(2);
private Settings() {
LOG = Logger.getLogger("Conf");
}
/**
* Used to retrieve a reference to this class.
*
* @return Returns the reference to this class (Singleton design pattern)
*/
public static Settings getInstance() {
if(ref == null) {
ref = new Settings();
}
return ref;
}
/*
private UPnPDevice loadUPnPDevice(int index) {
return XXX;
}
private boolean loadBooleanValue(int index) {
return XXX;
}
private void loadIntValue(int index) {
return XXX;
}
*/
public void loadSettings() {
try {
settingsStore = RecordStore.openRecordStore("ONEPP-Settings", true);
if (settingsStore.getNumRecords() == 0) {
LOG.debug("No records exist in RMS, making new records.");
//int res = -1;
// Adding 5 records
/*res = */settingsStore.addRecord(null, 0, 0);
/*res = */settingsStore.addRecord(null, 0, 0);
/*res = */settingsStore.addRecord(null, 0, 0);
/*res = */settingsStore.addRecord(null, 0, 0);
/*res = */settingsStore.addRecord(null, 0, 0);
}
else {
LOG.debug("Records exist in RMS, loading records.");
for (RecordEnumeration e =
settingsStore.enumerateRecords(null, null, false);
e.hasNextElement(); )
{
switch(e.nextRecordId()) {
case RECORDID_SETTING_MEDIASERVER: {
String s = "";
byte[] data = settingsStore.getRecord(RECORDID_SETTING_MEDIASERVER);
if (data != null && data.length > 0) {
s = new String(data,0,data.length);
int firstSeparatorIndex = -1;
int secondSeparatorIndex = -1;
for(int i = 0; i < s.length(); i++) {
if (s.substring(i,i+1).equals("|")) {
if (firstSeparatorIndex == -1) {
firstSeparatorIndex = i;
}
else {
secondSeparatorIndex = i;
}
}
}
String locationUrl = s.substring(firstSeparatorIndex+1, secondSeparatorIndex);
String UDN = s.substring(secondSeparatorIndex+1);
UI.getInstance().favouriteMediaServerUpdate(locationUrl, UDN);
/*
String locationUrl = s.substring(firstSeparatorIndex+1, secondSeparatorIndex);
System.out.println("Found locationUrl in RMS: " + locationUrl);
msList = new MediaServer[1];
msList[0] = msf.getMediaServer(locationUrl);
if(msList[0] == null) {
msList = null;
String UDN = s.substring(secondSeparatorIndex+1);
System.out.println("Found device UDN in RMS: " + UDN);
msList = msf.findMediaServers(UDN);
}
//UI.getInstance().updateMediaServerMenuElement();
*/
}
break;
}
case RECORDID_SETTING_MEDIARENDERER: {
String s = "";
byte[] data = settingsStore.getRecord(RECORDID_SETTING_MEDIARENDERER);
if (data != null && data.length > 0) {
s = new String(data,0,data.length);
// TODO Try to get the device description file
int firstSeparatorIndex = -1;
int secondSeparatorIndex = -1;
for(int i = 0; i < s.length(); i++) {
if (s.substring(i,i+1).equals("|")) {
if (firstSeparatorIndex == -1) {
firstSeparatorIndex = i;
}
else {
secondSeparatorIndex = i;
}
}
}
String locationUrl = s.substring(firstSeparatorIndex+1, secondSeparatorIndex);
String UDN = s.substring(secondSeparatorIndex+1);
UI.getInstance().favouriteMediaRendererUpdate(locationUrl, UDN);
/*
String locationUrl = s.substring(firstSeparatorIndex+1, secondSeparatorIndex);
System.out.println("Found locationUrl in RMS: " + locationUrl);
mrList = new MediaRenderer[1];
mrList[0] = mrf.getMediaRenderer(locationUrl);
if(mrList[0] == null) {
mrList = null;
String UDN = s.substring(secondSeparatorIndex+1);
System.out.println("Found device UDN in RMS: " + UDN);
mrList = mrf.findMediaRenderers(UDN);
}
*/
//UI.getInstance().updateMediaRendererMenuElement();
}
break;
}
case RECORDID_SETTING_REPEAT: {
byte[] data = settingsStore.getRecord(RECORDID_SETTING_REPEAT);
if (data != null && data.length > 0) {
repeat = Utilities.parseBoolean(new String(data,0,data.length));
}
break;
}
case RECORDID_SETTING_SHUFFLE: {
byte[] data = settingsStore.getRecord(RECORDID_SETTING_SHUFFLE);
if (data != null && data.length > 0) {
shuffle = Utilities.parseBoolean(new String(data, 0, data.length));
}
break;
}
case RECORDID_SETTING_HOSTINGPORT: {
byte[] data = settingsStore.getRecord(RECORDID_SETTING_HOSTINGPORT);
if (data != null && data.length > 0) {
LOG.debug("Parsing hosting port data");
hostingPort = Integer.parseInt(new String(data, 0, data.length));
}
break;
}
}
}
}
LOG.debug("Closing record store");
settingsStore.closeRecordStore();
} catch (RecordStoreException rse) {
LOG.debug("RS Exception...");
rse.printStackTrace();
}
}
private void saveUPnPDevice(int index, UPnPDevice dev) {
String s = "";
s = dev.getFriendlyName();
LOG.debug("Saving UPnPDevice: " + s + " to RMS");
s = s + "|" + dev.getDeviceDescriptionLocation();
s = s + "|" + dev.getDeviceUDN();
// XXX
System.out.println("Saving UPnPDevice: " + s + " to RMS");
byte[] data = s.getBytes();
try {
settingsStore.setRecord(
index,
data,
0,
data.length);
}
catch (RecordStoreNotOpenException e) {
LOG.fatal("Could not save UPnPDevice to RMS");
}
catch (InvalidRecordIDException e) {
LOG.fatal("Invalid record ID exception");
}
catch (RecordStoreException e) {
LOG.fatal("Record store exception");
}
}
private void saveBooleanValue(int index, boolean value) {
String s = "";
if (value == true) {
s = "true";
}
else if (value == false) {
s = "false";
}
byte[] data = s.getBytes();
try {
settingsStore.setRecord(
index,
data,
0,
data.length);
}
catch (RecordStoreNotOpenException e) {
LOG.fatal("Record store not open exception");
}
catch (InvalidRecordIDException e) {
LOG.fatal("Invalid record ID exception");
}
catch (RecordStoreException e) {
LOG.fatal("Record store exception");
}
}
private void saveIntValue(int index, int value) {
String s = "" + value;
byte[] data = s.getBytes();
try {
settingsStore.setRecord(
index,
data,
0,
data.length);
}
catch (RecordStoreNotOpenException e) {
LOG.fatal("Record store not open exception");
}
catch (InvalidRecordIDException e) {
LOG.fatal("Invalid record ID exception");
}
catch (RecordStoreException e) {
LOG.fatal("Record store exception");
}
}
/**
* Used to save settings to RMS
*/
public void saveSettings() {
if(settingsChanged == true) {
System.out.println("Saving settings");
try {
settingsStore = RecordStore.openRecordStore("ONEPP-Settings", true);
LOG.debug("Saving settings to RMS...");
for (RecordEnumeration e =
settingsStore.enumerateRecords(null, null, false);
e.hasNextElement(); )
{
int recordId = e.nextRecordId();
LOG.debug("Record id = " + recordId);
switch(recordId) {
case RECORDID_SETTING_MEDIASERVER: {
if(currentMediaServer >= 0 &&
currentMediaServer < msList.length)
{
MediaServer ms = msList[currentMediaServer];
saveUPnPDevice(RECORDID_SETTING_MEDIASERVER,ms);
}
break;
}
case RECORDID_SETTING_MEDIARENDERER: {
if(currentMediaRenderer >= 0 &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -