View Javadoc

1   package org.rpi.providers;
2   
3   import java.util.Observable;
4   import java.util.Observer;
5   import java.util.concurrent.CopyOnWriteArrayList;
6   
7   import org.apache.log4j.Logger;
8   import org.openhome.net.device.DvDevice;
9   import org.openhome.net.device.IDvInvocation;
10  import org.openhome.net.device.providers.DvProviderAvOpenhomeOrgProduct1;
11  import org.rpi.config.Config;
12  import org.rpi.player.PlayManager;
13  import org.rpi.player.events.EventBase;
14  import org.rpi.player.events.EventStandbyChanged;
15  import org.rpi.plugingateway.PluginGateWay;
16  import org.rpi.utils.Utils;
17  
18  public class PrvProduct extends DvProviderAvOpenhomeOrgProduct1 implements Observer {
19  
20  	private Logger log = Logger.getLogger(PrvProduct.class);
21  	private String friendly_name = Config.friendly_name;
22  	// private String iSourceXml =
23  	// "<SourceList><Source><Name>Playlist</Name><Type>Playlist</Type><Visible>1</Visible></Source><Source><Name>Receiver</Name><Type>Receiver</Type><Visible>1</Visible></Source><Source><Name>Radio</Name><Type>Radio</Type><Visible>1</Visible></Source></SourceList>";
24  	private String iSourceXml = "";
25  	// private boolean standby = true;
26  	private String attributes = "Info Time Volume";
27  	// private String attributes = "";
28  	private String man_name = "Java Inc";
29  	private String man_info = "Developed in Java using OpenHome and MPlayer";
30  	private String man_url = "";
31  	private String man_image = "";
32  	private String model_name = "Test Model Name";
33  	private String model_info = "Test Model Info";
34  	private String model_url = "";
35  	private String model_image = "";
36  	private String prod_room = friendly_name;
37  	private String prod_name = "Java MediaPlayer";
38  	private String prod_info = "Developed by Pete";
39  	private String prod_url = "";
40  	private String prod_image = "";
41  
42  	private PlayManager iPlayer = null;
43  
44  	private long iSourceXMLChangeCount = 0;
45  
46  	public PrvProduct(DvDevice iDevice) {
47  		super(iDevice);
48  		log.debug("Creating CustomProduct");
49  		iPlayer = PlayManager.getInstance();
50  		enablePropertyStandby();
51  		enablePropertyAttributes();
52  		enablePropertyManufacturerName();
53  		enablePropertyManufacturerInfo();
54  		enablePropertyManufacturerUrl();
55  		enablePropertyManufacturerImageUri();
56  		enablePropertyModelName();
57  		enablePropertyModelInfo();
58  		enablePropertyModelUrl();
59  		enablePropertyModelImageUri();
60  		enablePropertyProductRoom();
61  		enablePropertyProductName();
62  		enablePropertyProductInfo();
63  		enablePropertyProductUrl();
64  		enablePropertyProductImageUri();
65  		enablePropertySourceIndex();
66  		enablePropertySourceCount();
67  		enablePropertySourceXml();
68  
69  		setPropertyStandby(PlayManager.getInstance().isStandby());
70  		setPropertyAttributes(attributes);
71  
72  		setPropertyManufacturerName(man_name);
73  		setPropertyManufacturerInfo(man_info);
74  		setPropertyManufacturerUrl(man_url);
75  		setPropertyManufacturerImageUri(man_image);
76  
77  		setPropertyModelName(model_name);
78  		setPropertyModelInfo(model_info);
79  		setPropertyModelUrl(model_url);
80  		setPropertyModelImageUri(model_image);
81  
82  		setPropertyProductRoom(prod_room);
83  		setPropertyProductName(prod_name);
84  		setPropertyProductInfo(prod_info);
85  		setPropertyProductUrl(prod_url);
86  		setPropertyProductImageUri(prod_image);
87  
88  		setPropertySourceIndex(4);
89  		setPropertySourceCount(sources.size());
90  		setPropertySourceXml(iSourceXml);
91  
92  		enableActionManufacturer();
93  		enableActionModel();
94  		enableActionProduct();
95  		enableActionStandby();
96  		enableActionSetStandby();
97  		enableActionSourceCount();
98  		enableActionSourceXml();
99  		enableActionSourceIndex();
100 		enableActionSetSourceIndex();
101 		enableActionSetSourceIndexByName();
102 		enableActionSource();
103 		enableActionAttributes();
104 		enableActionSourceXmlChangeCount();
105 		PlayManager.getInstance().observeProductEvents(this);
106 		//initSources();
107 		setPropertySourceIndex(0);
108 
109 	}
110 
111 	private CopyOnWriteArrayList<Source> sources = new CopyOnWriteArrayList<Source>();
112 
113 	private void initSources() {
114 		addSource(Config.friendly_name, "Radio", "Radio", true);
115 		addSource(Config.friendly_name, "Playlist", "Playlist", true);
116 		if (Config.enableReceiver) {
117 			addSource(Config.friendly_name, "Receiver", "Receiver", true);
118 		}
119 		if (Config.enableAVTransport) {
120 			addSource(Config.friendly_name, "UpnpAV", "UpnpAv", false);
121 		}
122 	}
123 
124 	public void addSource(String system_name, String name, String type, boolean visible) {
125 		Source source = new Source(system_name, type, name, visible);
126 		sources.add(source);
127 		iSourceXMLChangeCount++;
128 		updateSourceXML();
129 	}
130 
131 	private void updateSourceXML() {
132 		StringBuffer sb = new StringBuffer();
133 		sb.append("<SourceList>");
134 		for (Source s : sources) {
135 			sb.append("<Source>");
136 			sb.append("<Name>");
137 			sb.append(s.getName());
138 			sb.append("</Name>");
139 			sb.append("<Type>");
140 			sb.append(s.getType());
141 			sb.append("</Type>");
142 			sb.append("<Visible>");
143 			sb.append(s.getVisible());
144 			sb.append("</Visible>");
145 			sb.append("</Source>");
146 		}
147 		sb.append("</SourceList>");
148 		iSourceXml = sb.toString();
149 		log.debug("SourceXML \r\n " + iSourceXml);
150 		propertiesLock();
151 		setPropertySourceCount(sources.size());
152 		setPropertySourceXml(iSourceXml.trim());
153 		propertiesUnlock();
154 	}
155 
156 	@Override
157 	protected void setStandby(IDvInvocation paramIDvInvocation, boolean paramBoolean) {
158 		log.debug("SetStandby: " + paramBoolean + Utils.getLogText(paramIDvInvocation));
159 		PlayManager.getInstance().setStandby(paramBoolean);
160 		if(paramBoolean==false)
161 		{
162 			updateCurrentSource();
163 		}
164 	}
165 
166 	@Override
167 	protected boolean standby(IDvInvocation paramIDvInvocation) {
168 		boolean standby = PlayManager.getInstance().isStandby();
169 		log.debug("GetStandby: " + standby + Utils.getLogText(paramIDvInvocation));
170 		return standby;
171 	}
172 
173 	@Override
174 	protected String attributes(IDvInvocation paramIDvInvocation) {
175 		log.debug("Attributes: " + attributes + Utils.getLogText(paramIDvInvocation));
176 		return attributes;
177 	}
178 
179 	@Override
180 	protected Manufacturer manufacturer(IDvInvocation paramIDvInvocation) {
181 		log.debug("Manufacturer" + Utils.getLogText(paramIDvInvocation));
182 		Manufacturer man = new Manufacturer(man_name, man_info, man_url, man_image);
183 		return man;
184 	}
185 
186 	@Override
187 	protected Model model(IDvInvocation paramIDvInvocation) {
188 		log.debug("Model" + Utils.getLogText(paramIDvInvocation));
189 		Model model = new Model(model_name, model_info, model_url, model_image);
190 		return model;
191 	}
192 
193 	@Override
194 	protected Product product(IDvInvocation paramIDvInvocation) {
195 		log.debug("Product" + Utils.getLogText(paramIDvInvocation));
196 		Product product = new Product(Config.friendly_name, prod_name, prod_info, prod_url, prod_image);
197 		return product;
198 	}
199 
200 	@Override
201 	protected Source source(IDvInvocation paramIDvInvocation, long iD) {
202 		log.debug("Source: " + iD + Utils.getLogText(paramIDvInvocation));
203 		if (sources.size() >= iD) {
204 			try {
205 				Source s = sources.get((int) iD);
206 				return s;
207 			} catch (Exception e) {
208 				log.error("Error GetSource: " + e);
209 			}
210 		}
211 		return null;
212 	}
213 
214 	@Override
215 	protected long sourceCount(IDvInvocation paramIDvInvocation) {
216 		long source_count = getPropertySourceCount();
217 		log.debug("SourceCount: " + source_count + Utils.getLogText(paramIDvInvocation));
218 		return source_count;
219 	}
220 
221 	@Override
222 	protected void setSourceIndex(IDvInvocation paramIDvInvocation, long paramLong) {
223 		log.debug("SetSourceIndex: " + paramLong + Utils.getLogText(paramIDvInvocation));
224 		Source source = sources.get((int)paramLong);
225 		String name = source.getName();
226 		log.debug("Source Selected: " + name);
227 		setPropertySourceIndex(paramLong);
228 		PluginGateWay.getInstance().setSourceId(name);
229 		
230 	}
231 
232 	@Override
233 	protected long sourceIndex(IDvInvocation paramIDvInvocation) {
234 		long source_index = getPropertySourceIndex();
235 		log.debug("SourceIndex: " + source_index + Utils.getLogText(paramIDvInvocation));
236 		return source_index;
237 	}
238 
239 	@Override
240 	protected String sourceXml(IDvInvocation paramIDvInvocation) {
241 		log.debug("SourceXML: " + iSourceXml + Utils.getLogText(paramIDvInvocation));
242 		return iSourceXml;
243 	}
244 
245 	@Override
246 	protected long sourceXmlChangeCount(IDvInvocation paramIDvInvocation) {
247 		log.debug("SourceXmlChangeCount: " + iSourceXMLChangeCount + Utils.getLogText(paramIDvInvocation));
248 		return iSourceXMLChangeCount;
249 	}
250 
251 	@Override
252 	protected void setSourceIndexByName(IDvInvocation paramIDvInvocation, String paramString) {
253 		log.debug("SetSourceIndexByName: " + paramString + Utils.getLogText(paramIDvInvocation));
254 		setSourceByname(paramString);
255 	}
256 
257 	public void updateStandby(boolean standby) {
258 		propertiesLock();
259 		setPropertyStandby(standby);
260 		propertiesUnlock();
261 	}
262 
263 	/**
264 	 * Attempt to try and set the PlayList as Default Source
265 	 * 
266 	 * @param paramLong
267 	 */
268 	public synchronized void setSourceId(long paramLong) {
269 		setPropertySourceIndex(paramLong);
270 	}
271 
272 	public synchronized void setSourceByname(String name) {
273 		long count = 0;
274 		for (Source source : sources) {
275 			if (source.getName().equalsIgnoreCase(name)) {
276 				setPropertySourceIndex(count);
277 			}
278 			count++;
279 		}
280 	}
281 
282 	@Override
283 	public void update(Observable o, Object arg) {
284 		EventBase e = (EventBase) arg;
285 		switch (e.getType()) {
286 		case EVENTSTANDBYCHANGED:
287 			EventStandbyChanged ev = (EventStandbyChanged) e;
288 			updateStandby(ev.isStandby());
289 			break;
290 		}
291 
292 	}
293 
294 	/**
295 	 * Test to see of the initial Source can be obtained.
296 	 */
297 	public void updateCurrentSource() {
298 		try
299 		{
300 			long source_index = getPropertySourceIndex();
301 			Source source = sources.get((int)source_index);
302 			String name = source.getName();
303 			log.debug("Source Selected: " + name);
304 			PluginGateWay.getInstance().setSourceId(name);
305 		}
306 		catch(Exception e)
307 		{
308 			log.error(e);
309 		}
310 		
311 	}
312 }