00001 00009 package org.rosmultimedia.player.xbmc.internal; 00010 00011 import org.rosbuilding.common.media.ISpeaker; 00012 import org.rosmultimedia.player.xbmc.XbmcNode; 00013 import org.rosmultimedia.player.xbmc.jsonrpc.XbmcJson; 00014 import org.xbmc.android.jsonrpc.api.call.Application; 00015 import org.xbmc.android.jsonrpc.api.model.ApplicationModel.PropertyValue; 00016 import org.xbmc.android.jsonrpc.api.model.GlobalModel.Toggle; 00017 00018 import smarthome_media_msgs.MediaAction; 00019 import smarthome_media_msgs.SpeakerInfo; 00020 import smarthome_media_msgs.StateData; 00021 import smarthome_media_msgs.ToggleMuteSpeakerRequest; 00022 import smarthome_media_msgs.ToggleMuteSpeakerResponse; 00023 00030 public class XbmcSpeaker implements ISpeaker { 00034 private XbmcNode xbmcNode; 00035 00039 private XbmcJson xbmcJson; 00040 00046 public XbmcSpeaker(XbmcJson xbmcjson, XbmcNode xbmcNode) { 00047 this.xbmcJson = xbmcjson; 00048 this.xbmcNode = xbmcNode; 00049 } 00050 00051 @Override 00052 public void load(StateData stateData) { 00053 this.load(stateData.getSpeaker()); 00054 } 00055 00056 public void load(SpeakerInfo speakerInfo) { 00057 PropertyValue property = this.xbmcJson.getResult( 00058 new Application.GetProperties( 00059 PropertyValue.MUTED, 00060 PropertyValue.NAME, 00061 PropertyValue.VERSION, 00062 PropertyValue.VOLUME)); 00063 00064 if (property != null) { 00065 speakerInfo.setMuted(property.muted); 00066 speakerInfo.setLevel(property.volume); 00067 } 00068 } 00069 00070 @Override 00071 public void callbackCmdAction(MediaAction message, StateData stateData) { 00072 switch (message.getMethod()) { 00073 case OP_MUTE: 00074 case OP_MUTE_TOGGLE: 00075 this.xbmcJson.getResult(new Application.SetMute( 00076 new Toggle(!stateData.getSpeaker().getMuted()))); 00077 break; 00078 case OP_VOLUME_DOWN: 00079 int level = stateData.getSpeaker().getLevel() - LEVEL_STEP; 00080 00081 if (level < LEVEL_MIN) { 00082 level = LEVEL_MIN; 00083 } 00084 00085 this.xbmcJson.getResult(new Application.SetVolume(level)); 00086 break; 00087 case OP_VOLUME_UP: 00088 level = stateData.getSpeaker().getLevel() + LEVEL_STEP; 00089 00090 if (level > LEVEL_MAX) { 00091 level = LEVEL_MAX; 00092 } 00093 00094 this.xbmcJson.getResult(new Application.SetVolume(level)); 00095 break; 00096 case OP_VOLUME_TO: 00097 level = 50; 00098 00099 if (level > LEVEL_MIN && level < LEVEL_MAX) { 00100 this.xbmcJson.getResult(new Application.SetVolume(level)); 00101 } 00102 break; 00103 } 00104 } 00105 00106 @Override 00107 public void handleSpeakerMuteToggle(ToggleMuteSpeakerRequest request, 00108 ToggleMuteSpeakerResponse response) { 00109 response.setState(!this.xbmcNode.getStateData().getSpeaker().getMuted()); 00110 00111 this.xbmcNode.logI(String.format("Service call %s : %s", 00112 XbmcNode.SRV_MUTE_SPEAKER_TOGGLE, 00113 this.xbmcNode.getStateData().getSpeaker().getMuted())); 00114 00115 MediaAction message = this.xbmcNode.getConnectedNode().getTopicMessageFactory() 00116 .newFromType(MediaAction._TYPE); 00117 00118 message.setMethod(XbmcSpeaker.OP_MUTE_TOGGLE); 00119 00120 this.callbackCmdAction(message, this.xbmcNode.getStateData()); 00121 } 00122 }