⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settings.java

📁 国外的j2me播放器软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package no.auc.one.portableplayer.settings;

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.utils.Utilities;

import java.io.IOException;
import java.util.*;

import javax.microedition.midlet.MIDlet;
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 Hashtable mediaServers = new Hashtable(3);
    private String currentMediaServer = null;
    	
	private MediaRendererFactory mrf = new MediaRendererFactory();
	private Hashtable mediaRenderers = new Hashtable(3);
    private String currentMediaRenderer = null;
	
    private boolean repeat = false;
    private boolean shuffle = false;
    private int hostingPort = -1;
    private int manuallySelectedHostingPort = -1;
    private boolean settingsChanged = false;
    
    private static final int SETTING_MEDIASERVER = 1;
    private static final int SETTING_MEDIARENDERER = 2;
    private static final int SETTING_SHUFFLE = 3;
    private static final int SETTING_REPEAT = 4;
    private static final int SETTING_HOSTINGPORT = 5;
    private RecordStore settingsStore = null;
    
    private static final String SETTINGS_STORE_NAME = "ONEPP-Settings";
    
    Vector settingsChangedListeners = new Vector(2);
    private static MIDlet midlet;
	
	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() {
		return ref;
	}
    
    public static Settings createInstance(MIDlet m) {
        if (ref == null) {
            midlet = m;
            ref = new Settings();
        } else {
            throw new IllegalStateException("Ref already exists..."); // TODO Fix with better exception!
        }
        
        return ref;
    }
    
	/*
	private UPnPDevice loadUPnPDevice(int index) {
		return xyz;
	}
	private boolean loadBooleanValue(int index) {
		return xyz;
	}
	private void loadIntValue(int index) {
		return xyz;
	}
	*/
	public void loadSettings() {
        Thread loadMediaServerSetting = null;
        Thread loadMediaRendererSetting = null;
        String favouriteMsUdn = null;
        String favouriteMrUdn = null;
        
        //
        // See if there are any devices to load from the JAD file
        //
        for (int i = 1; i < 10; i++) {
            String location = midlet.getAppProperty(
                "ONEPP-MediaServer-" + i);
            
            if (location != null) {
                addMediaServerByLocationUrl(location);
            } else {
                break;
            }
        }
        
        for (int i = 1; i < 10; i++) {
            String location = midlet.getAppProperty(
                "ONEPP-MediaRenderer-" + i);
            
            if (location != null) {
                addMediaRendererByLocationUrl(location);
            } else {
                break;
            }
        }       
        
		try {
            settingsStore = RecordStore.openRecordStore(
                    SETTINGS_STORE_NAME, 
                    true);
			
            if (settingsStore.getNumRecords() == 0) {
				System.err.println("No records exist in RMS, making new records.");
                
                settingsStore.addRecord(null, 0, 0);
                settingsStore.addRecord(null, 0, 0);
                settingsStore.addRecord(null, 0, 0);
                settingsStore.addRecord(null, 0, 0);
                settingsStore.addRecord(null, 0, 0);
            } else {
				System.out.println("Records exist in RMS, loading records.");
                for (RecordEnumeration e = 
                     settingsStore.enumerateRecords(null, null, false);
                     e.hasNextElement(); )
                {
                    try {
                        int currentRecordId = e.nextRecordId();
                        byte[] data = settingsStore.getRecord(currentRecordId);
                        if (data == null || data.length <= 0) {
                            continue;
                        }
                        
                        System.err.println("Setting: " + currentRecordId);
                        
                        switch (currentRecordId) {
                        case SETTING_MEDIASERVER: {
                            System.err.print("SETTING_MEDIASERVER: ");
                            String s = new String(data, 0, data.length);
                            System.err.println(s);
    
                            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);
                            favouriteMsUdn = s.substring(secondSeparatorIndex + 1);
                            System.err.println("Starting thread...");
                            loadMediaServerSetting = new Thread(
                                    new LoadDeviceSetting(
                                            SETTING_MEDIASERVER,
                                            locationUrl,
                                            favouriteMsUdn));
                            loadMediaServerSetting.start();
                            System.err.println("Thread started!");
                        }
                        break;
    
                        case SETTING_MEDIARENDERER: {
                            String 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);
                            favouriteMrUdn = s.substring(secondSeparatorIndex + 1);
                            loadMediaRendererSetting = new Thread(
                                    new LoadDeviceSetting(
                                            SETTING_MEDIARENDERER,
                                            locationUrl,
                                            favouriteMrUdn));
                            loadMediaRendererSetting.start();
                            break;
                        }
    
                        case SETTING_REPEAT:
                            setRepeatSetting(
                                Utilities.parseBoolean(new String(
                                    data, 
                                    0, 
                                    data.length)));
                        break;
    
                        case SETTING_SHUFFLE:
                            setShuffleSetting(
                                Utilities.parseBoolean(new String(
                                    data,
                                    0, 
                                    data.length)));
                        break;
    
                        case SETTING_HOSTINGPORT:
                            LOG.debug("Parsing hosting port data");
                            hostingPort = Integer.parseInt(new String(data, 0,
                                            data.length));
                        break;
                        }
                    } catch (Exception ee) {
                        System.err.println("Error loading setting: " + e);
                        ee.printStackTrace();
                    }
                }
                
                if (loadMediaServerSetting != null) {
                    try {
                        System.err.println("Join loadMediaServerSetting");
                        loadMediaServerSetting.join();
                    } catch (InterruptedException e) {
                        // Do nothing
                    } finally {
                        System.err.println("Joined loadMediaServerSetting");
                    }
                }
                
                if (loadMediaRendererSetting != null) {
                    try {
                        System.err.println("Join loadMediaRenSetting");
                        loadMediaRendererSetting.join();
                    } catch (InterruptedException e) {
                        // Do nothing
                    } finally {
                        System.err.println("Joined loadMediaRendererSetting");
                    }
                }
                
                //
                // Update the Media Server/Renderer menu
                //
                UI ui = UI.getInstance();                    

                try {
                    ui.updateMediaServerMenu(false).join();
                    ui.updateMediaRendererMenu(false).join();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                //
                // Set the current Media Server device
                // 
                if (favouriteMsUdn != null && 
                    mediaServers.containsKey(favouriteMsUdn)) 
                {
                    setCurrentMediaServer(favouriteMsUdn);
                    ui.setCurrentMediaServer();
                } else if (mediaServers.containsKey(
                    no.auc.one.portableplayer.lms.ServerDevice.getInstance().udn())) 
                {
                    setCurrentMediaServer(
                        no.auc.one.portableplayer.lms.ServerDevice.getInstance().udn());
                    ui.setCurrentMediaServer();
                }               
                
                //
                // Set the current Media Renderer device
                //
                if(favouriteMrUdn != null && 
                   mediaRenderers.containsKey(favouriteMrUdn)) 
                {
                    setCurrentMediaRendererUdn(favouriteMrUdn);
                    ui.setCurrentMediaRenderer();
                } else if (mediaRenderers.containsKey(
                    no.auc.one.portableplayer.lmr.MediaRenderer.getInstance().udn())) 
                {
                    setCurrentMediaRendererUdn(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -