001package org.intellimate.izou.sdk.frameworks.music; 002 003import org.intellimate.izou.resource.ResourceModel; 004import org.intellimate.izou.sdk.Context; 005 006import java.util.HashMap; 007import java.util.Optional; 008 009/** 010 * This class has information about the capabilities of the Music-Player. The default is always false. 011 * @author LeanderK 012 * @version 1.0 013 */ 014@SuppressWarnings("WeakerAccess") 015public class Capabilities { 016 @SuppressWarnings("SpellCheckingInspection") 017 protected static final String playPauseControlDescriptor = "izou.music.capabilities.playpausecontrol"; 018 private boolean playPauseControl = false; 019 @SuppressWarnings("SpellCheckingInspection") 020 protected static final String playRequestOutsideDescriptor = "izou.music.capabilities.playrequestoutside"; 021 private boolean playRequestOutside = false; 022 @SuppressWarnings("SpellCheckingInspection") 023 protected static final String playRequestDetailedDescriptor = "izou.music.capabilities.playrequestdetailed"; 024 private boolean playRequestDetailed = false; 025 @SuppressWarnings("SpellCheckingInspection") 026 protected static final String providesTrackInfoDescriptor = "izou.music.capabilities.providestrackinfo"; 027 private boolean providesTrackInfo = false; 028 @SuppressWarnings("SpellCheckingInspection") 029 protected static final String ableToSelectTrackDescriptor = "izou.music.capabilities.abletoselecttrack"; 030 private boolean ableToSelectTrack = false; 031 @SuppressWarnings("SpellCheckingInspection") 032 protected static final String nextPreviousDescriptor = "izou.music.capabilities.nextprevious"; 033 private boolean nextPrevious = false; 034 protected static final String ableToJumpDescriptor = "izou.music.capabilities.jump"; 035 private boolean ableToJump = false; 036 protected static final String playbackShuffleDescriptor = "izou.music.capabilities.playback.shuffle"; 037 private boolean playbackShuffle = false; 038 protected static final String playbackRepeatDescriptor = "izou.music.capabilities.playback.repeat"; 039 private boolean playbackRepeat = false; 040 @SuppressWarnings("SpellCheckingInspection") 041 protected static final String playbackRepeatSongDescriptor = "izou.music.capabilities.playback.repeatsong"; 042 private boolean playbackRepeatSong = false; 043 @SuppressWarnings("SpellCheckingInspection") 044 protected static final String playbackChangeableDescriptor = "izou.music.capabilities.playback.changable"; 045 private boolean playbackChangeable = false; 046 protected static final String changeVolumeDescriptor = "izou.music.capabilities.volume"; 047 private boolean changeVolume = false; 048 @SuppressWarnings("SpellCheckingInspection") 049 protected static final String broadcastingDescriptor = "izou.music.capabilities.broadcasting"; 050 private boolean broadcasting = false; 051 052 /** 053 * creates a new Context 054 * @param context the context to use 055 * @deprecated context is not used anymore 056 */ 057 @Deprecated 058 public Capabilities(Context context) { 059 } 060 061 public Capabilities() { 062 063 } 064 065 /** 066 * whether you can change the Volume from outside the player 067 * @return true if able to, false if not 068 */ 069 public boolean canChangeVolume() { 070 return changeVolume; 071 } 072 073 /** 074 * sets whether you can change the Volume from outside the player 075 * @param changeVolume true if the player is capable, false if not 076 */ 077 public void setChangeVolume(boolean changeVolume) { 078 this.changeVolume = changeVolume; 079 } 080 081 /** 082 * whether you can request playing from outside addons 083 * @return true if able to, false if not 084 */ 085 public boolean handlesPlayRequestFromOutside() { 086 return playRequestOutside; 087 } 088 089 /** 090 * sets whether you can request playing from outside addons 091 * @param playRequestOutside true if the player is capable, false if not 092 */ 093 public void setPlayRequestOutside(boolean playRequestOutside) { 094 this.playRequestOutside = playRequestOutside; 095 } 096 097 /** 098 * whether you can pause the playback without ending the player 099 * @return true if able to, false if not 100 */ 101 public boolean hasPlayPauseControl() { 102 return playPauseControl; 103 } 104 105 /** 106 * sets whether you can pause the playback without ending the player 107 * @param playPauseControl true if the player is capable, false if not 108 */ 109 public void setPlayPauseControl(boolean playPauseControl) { 110 this.playPauseControl = playPauseControl; 111 } 112 113 /** 114 * whether you can request a track with providing a TrackInfo/Playlist 115 * @return true if able to, false if not 116 */ 117 public boolean hasPlayRequestDetailed() { 118 return playRequestDetailed; 119 } 120 121 /** 122 * sets whether you can request a track with providing a TrackInfo/Playlist 123 * @param playRequestDetailed true if the player is capable, false if not 124 */ 125 public void setPlayRequestDetailed(boolean playRequestDetailed) { 126 this.playRequestDetailed = playRequestDetailed; 127 } 128 129 /** 130 * returns whether the player may provide TrackInfo 131 * @return true if able to, false if not 132 */ 133 public boolean providesTrackInfo() { 134 return providesTrackInfo; 135 } 136 137 /** 138 * sets whether you may provide a trackinfo 139 * @param providesTrackInfo true if the player is capable, false if not 140 */ 141 public void setProvidesTrackInfo(boolean providesTrackInfo) { 142 this.providesTrackInfo = providesTrackInfo; 143 } 144 145 /** 146 * whether one is able to select a Track 147 * @return true if able, false if not 148 */ 149 public boolean isAbleToSelectTrack() { 150 return ableToSelectTrack; 151 } 152 153 /** 154 * sets whether one is able to select a Track 155 * @param ableToSelectTrack true if able, false if not 156 */ 157 public void setAbleToSelectTrack(boolean ableToSelectTrack) { 158 this.ableToSelectTrack = ableToSelectTrack; 159 } 160 161 /** 162 * whether one is able to select the next/previous track 163 * @return true if able, false if not 164 */ 165 public boolean hasNextPrevious() { 166 return nextPrevious; 167 } 168 169 /** 170 * sets whether one is able to select the next/previous track 171 * @param nextPrevious true if able, false if not 172 */ 173 public void setNextPrevious(boolean nextPrevious) { 174 this.nextPrevious = nextPrevious; 175 } 176 177 /** 178 * whether one is able to jump (select the current position of the track) 179 * @return true if able, false if not 180 */ 181 public boolean isAbleToJump() { 182 return ableToJump; 183 } 184 185 /** 186 * sets whether one is able to jump 187 * @param ableToJump true if able, false if not 188 */ 189 public void setAbleToJump(boolean ableToJump) { 190 this.ableToJump = ableToJump; 191 } 192 193 /** 194 * whether one is able to shuffle songs in a playlist 195 * @return true if able, false if not 196 */ 197 public boolean canRepeatPlayback() { 198 return playbackRepeat; 199 } 200 201 /** 202 * sets whether the player is able to shuffle songs in a playlist 203 * @param playbackRepeat true if able, false if not 204 */ 205 public void setPlaybackRepeat(boolean playbackRepeat) { 206 this.playbackRepeat = playbackRepeat; 207 } 208 209 /** 210 * whether one is able to repeat songs in a playlist 211 * @return true if able, false if not 212 */ 213 public boolean canRepeatPlaybackOfSong() { 214 return playbackRepeatSong; 215 } 216 217 /** 218 * sets whether one is able to repeat songs in a playlist 219 * @param playbackRepeatSong true if able, false if not 220 */ 221 public void setPlaybackRepeatSong(boolean playbackRepeatSong) { 222 this.playbackRepeatSong = playbackRepeatSong; 223 } 224 225 /** 226 * whether one is able to change the playback 227 * @return true if able, false if not 228 */ 229 public boolean isPlaybackChangeable() { 230 return playbackChangeable; 231 } 232 233 /** 234 * sets whether one is able to change the playback 235 * @param playbackChangeable true if able, false if not 236 */ 237 public void setPlaybackChangeable(boolean playbackChangeable) { 238 this.playbackChangeable = playbackChangeable; 239 } 240 241 /** 242 * whether one is able to shuffle the playback 243 * @return true if able, false if not 244 */ 245 public boolean canShufflePlayback() { 246 return playbackShuffle; 247 } 248 249 /** 250 * sets whether one is able to shuffle the playback 251 * @param playbackShuffle true if able, false if not 252 */ 253 public void setPlaybackShuffle(boolean playbackShuffle) { 254 this.playbackShuffle = playbackShuffle; 255 } 256 257 /** 258 * returns whether you can obtain available Playlists via the BroadcasterAvailablePlaylists-resource. 259 * @return true if able, false if not 260 */ 261 public boolean isBroadcasting() { 262 return broadcasting; 263 } 264 265 /** 266 * sets whether you can obtain available Playlists via the BroadcasterAvailablePlaylists-resource. 267 * @param broadcasting true if able, false if not 268 */ 269 public void setBroadcasting(boolean broadcasting) { 270 this.broadcasting = broadcasting; 271 } 272 273 public HashMap<String, Boolean> write() { 274 HashMap<String, Boolean> data = new HashMap<>(); 275 data.put(playPauseControlDescriptor, playPauseControl); 276 data.put(playRequestDetailedDescriptor, playRequestDetailed); 277 data.put(providesTrackInfoDescriptor, providesTrackInfo); 278 data.put(ableToSelectTrackDescriptor, ableToSelectTrack); 279 data.put(nextPreviousDescriptor, nextPrevious); 280 data.put(ableToJumpDescriptor, ableToJump); 281 data.put(playbackChangeableDescriptor, playbackChangeable); 282 data.put(playbackRepeatDescriptor, playbackRepeat); 283 data.put(playbackRepeatSongDescriptor, playbackRepeatSong); 284 data.put(playbackShuffleDescriptor, playbackShuffle); 285 data.put(playRequestOutsideDescriptor, playRequestOutside); 286 data.put(changeVolumeDescriptor, changeVolume); 287 data.put(broadcastingDescriptor, broadcasting); 288 return data; 289 } 290 291 public static Capabilities constructCapabilites(HashMap<String, Boolean> data, Context context) { 292 Capabilities capabilities = new Capabilities(); 293 data.keySet().forEach(descriptor -> { 294 switch (descriptor) { 295 case playPauseControlDescriptor: capabilities.setPlayPauseControl(data.get(descriptor)); 296 break; 297 case playRequestDetailedDescriptor: capabilities.setPlayRequestDetailed(data.get(descriptor)); 298 break; 299 case providesTrackInfoDescriptor: capabilities.setProvidesTrackInfo(data.get(descriptor)); 300 break; 301 case ableToSelectTrackDescriptor: capabilities.setAbleToSelectTrack(data.get(descriptor)); 302 break; 303 case nextPreviousDescriptor: capabilities.setNextPrevious(data.get(descriptor)); 304 break; 305 case playbackChangeableDescriptor: capabilities.setPlaybackChangeable(data.get(descriptor)); 306 break; 307 case playbackRepeatDescriptor: capabilities.setPlaybackRepeat(data.get(descriptor)); 308 break; 309 case playbackRepeatSongDescriptor: capabilities.setPlaybackRepeatSong(data.get(descriptor)); 310 break; 311 case playbackShuffleDescriptor: capabilities.setPlaybackShuffle(data.get(descriptor)); 312 break; 313 case playRequestOutsideDescriptor: capabilities.setPlayRequestOutside(data.get(descriptor)); 314 break; 315 case changeVolumeDescriptor: capabilities.setChangeVolume(data.get(descriptor)); 316 break; 317 case ableToJumpDescriptor: capabilities.setAbleToJump(data.get(descriptor)); 318 break; 319 case broadcastingDescriptor: capabilities.setBroadcasting(data.get(descriptor)); 320 break; 321 default: context.getLogger().error("unkown command: " + descriptor); 322 break; 323 } 324 }); 325 return capabilities; 326 } 327 328 /** 329 * creates a TrackInfo from the resourceModel 330 * @param resourceModel the resourceModel 331 * @return the optional TrackInfo 332 */ 333 public static Optional<Capabilities> importFromResource(ResourceModel resourceModel, Context context) { 334 Object resource = resourceModel.getResource(); 335 try { 336 @SuppressWarnings("unchecked") 337 HashMap<String, Boolean> hashMap = (HashMap<String, Boolean>) resource; 338 return Optional.of(constructCapabilites(hashMap, context)); 339 } catch (ClassCastException e) { 340 return Optional.empty(); 341 } 342 } 343}