PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Squeezer/src/main/java/uk/org/ngo/squeezer/model/PlayerState.java

https://github.com/kaaholst/android-squeezer
Java | 521 lines | 369 code | 117 blank | 35 comment | 36 complexity | bc953feb362020f3f6a6351d4418da91 MD5 | raw file
  1. /*
  2. * Copyright (c) 2009 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package uk.org.ngo.squeezer.model;
  17. import android.os.Parcel;
  18. import android.os.Parcelable;
  19. import android.os.SystemClock;
  20. import androidx.annotation.DrawableRes;
  21. import androidx.annotation.NonNull;
  22. import androidx.annotation.Nullable;
  23. import androidx.annotation.StringDef;
  24. import java.lang.annotation.Retention;
  25. import java.lang.annotation.RetentionPolicy;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import uk.org.ngo.squeezer.R;
  31. import uk.org.ngo.squeezer.Util;
  32. import uk.org.ngo.squeezer.framework.EnumIdLookup;
  33. import uk.org.ngo.squeezer.framework.EnumWithId;
  34. public class PlayerState implements Parcelable {
  35. public PlayerState() {
  36. }
  37. public static final Creator<PlayerState> CREATOR = new Creator<PlayerState>() {
  38. @Override
  39. public PlayerState[] newArray(int size) {
  40. return new PlayerState[size];
  41. }
  42. @Override
  43. public PlayerState createFromParcel(Parcel source) {
  44. return new PlayerState(source);
  45. }
  46. };
  47. private PlayerState(Parcel source) {
  48. playStatus = source.readString();
  49. poweredOn = (source.readByte() == 1);
  50. shuffleStatus = ShuffleStatus.valueOf(source.readInt());
  51. repeatStatus = RepeatStatus.valueOf(source.readInt());
  52. currentSong = source.readParcelable(getClass().getClassLoader());
  53. currentPlaylist = source.readString();
  54. currentPlaylistTimestamp = source.readLong();
  55. currentPlaylistIndex = source.readInt();
  56. currentTimeSecond = source.readDouble();
  57. currentSongDuration = source.readInt();
  58. currentVolume = source.readInt();
  59. sleepDuration = source.readInt();
  60. sleep = source.readInt();
  61. mSyncMaster = source.readString();
  62. source.readStringList(mSyncSlaves);
  63. mPlayerSubscriptionType = PlayerSubscriptionType.valueOf(source.readString());
  64. prefs = source.readHashMap(getClass().getClassLoader());
  65. mPlayR =(source.readByte() == 1);
  66. }
  67. @Override
  68. public void writeToParcel(Parcel dest, int flags) {
  69. dest.writeString(playStatus);
  70. dest.writeByte(poweredOn ? (byte) 1 : (byte) 0);
  71. dest.writeInt(shuffleStatus.getId());
  72. dest.writeInt(repeatStatus.getId());
  73. dest.writeParcelable(currentSong, flags);
  74. dest.writeString(currentPlaylist);
  75. dest.writeLong(currentPlaylistTimestamp);
  76. dest.writeInt(currentPlaylistIndex);
  77. dest.writeDouble(currentTimeSecond);
  78. dest.writeInt(currentSongDuration);
  79. dest.writeInt(currentVolume);
  80. dest.writeInt(sleepDuration);
  81. dest.writeDouble(sleep);
  82. dest.writeString(mSyncMaster);
  83. dest.writeStringList(mSyncSlaves);
  84. dest.writeString(mPlayerSubscriptionType.name());
  85. dest.writeMap(prefs);
  86. dest.writeByte(mPlayR ? (byte) 1 : (byte) 0);
  87. }
  88. @Override
  89. public int describeContents() {
  90. return 0;
  91. }
  92. private boolean poweredOn;
  93. private @PlayState String playStatus;
  94. private ShuffleStatus shuffleStatus;
  95. private RepeatStatus repeatStatus;
  96. private CurrentPlaylistItem currentSong;
  97. /** The name of the current playlist, which may be the empty string. */
  98. @NonNull
  99. private String currentPlaylist;
  100. private long currentPlaylistTimestamp;
  101. private int currentPlaylistTracksNum;
  102. private int currentPlaylistIndex;
  103. private boolean remote;
  104. public boolean waitingToPlay;
  105. public double rate;
  106. private double currentTimeSecond;
  107. private int currentSongDuration;
  108. public double statusSeen;
  109. private int currentVolume = 101;
  110. private int sleepDuration;
  111. private double sleep;
  112. /** Is the player playing the tracks of a filesystem folder randomly (via context menu) **/
  113. private boolean mPlayR;
  114. /** The player this player is synced to (null if none). */
  115. @Nullable
  116. private String mSyncMaster;
  117. /** The players synced to this player. */
  118. private List<String> mSyncSlaves = Collections.emptyList();
  119. /** How the server is subscribed to the player's status changes. */
  120. @NonNull
  121. private PlayerSubscriptionType mPlayerSubscriptionType = PlayerSubscriptionType.NOTIFY_NONE;
  122. /** Map of current values of our the playerprefs we track. See the specific SlimClient */
  123. @NonNull
  124. public Map<Player.Pref, String> prefs = new HashMap<>();
  125. public boolean isPlaying() {
  126. return PLAY_STATE_PLAY.equals(playStatus);
  127. }
  128. /**
  129. * @return the player's state. May be null, which indicates that Squeezer has received
  130. * a "players" response for this player, but has not yet received a status message
  131. * for it.
  132. */
  133. @Nullable
  134. @PlayState
  135. public String getPlayStatus() {
  136. return playStatus;
  137. }
  138. public boolean setPlayStatus(@NonNull @PlayState String s) {
  139. if (s.equals(playStatus)) {
  140. return false;
  141. }
  142. playStatus = s;
  143. return true;
  144. }
  145. public boolean isPoweredOn() {
  146. return poweredOn;
  147. }
  148. public boolean setPoweredOn(boolean state) {
  149. if (state == poweredOn)
  150. return false;
  151. poweredOn = state;
  152. return true;
  153. }
  154. public ShuffleStatus getShuffleStatus() {
  155. return shuffleStatus;
  156. }
  157. public boolean setShuffleStatus(ShuffleStatus status) {
  158. if (status == shuffleStatus)
  159. return false;
  160. shuffleStatus = status;
  161. return true;
  162. }
  163. public boolean setShuffleStatus(String s) {
  164. return setShuffleStatus(s != null ? ShuffleStatus.valueOf(Util.getInt(s)) : null);
  165. }
  166. public RepeatStatus getRepeatStatus() {
  167. return repeatStatus;
  168. }
  169. public boolean setRepeatStatus(RepeatStatus status) {
  170. if (status == repeatStatus)
  171. return false;
  172. repeatStatus = status;
  173. return true;
  174. }
  175. public boolean setRepeatStatus(String s) {
  176. return setRepeatStatus(s != null ? RepeatStatus.valueOf(Util.getInt(s)) : null);
  177. }
  178. public CurrentPlaylistItem getCurrentSong() {
  179. return currentSong;
  180. }
  181. public boolean setCurrentSong(CurrentPlaylistItem song) {
  182. if (song.equals(currentSong))
  183. return false;
  184. currentSong = song;
  185. return true;
  186. }
  187. /** @return the name of the current playlist, may be the empty string. */
  188. @NonNull
  189. public String getCurrentPlaylist() {
  190. return currentPlaylist;
  191. }
  192. public long getCurrentPlaylistTimestamp() {
  193. return currentPlaylistTimestamp;
  194. }
  195. public boolean setCurrentPlaylistTimestamp(long value) {
  196. if (value == currentPlaylistTimestamp)
  197. return false;
  198. currentPlaylistTimestamp = value;
  199. return true;
  200. }
  201. /** @return the number of tracks in the current playlist */
  202. public int getCurrentPlaylistTracksNum() {
  203. return currentPlaylistTracksNum;
  204. }
  205. public int getCurrentPlaylistIndex() {
  206. return currentPlaylistIndex;
  207. }
  208. public void setCurrentPlaylist(@Nullable String playlist) {
  209. if (playlist == null)
  210. playlist = "";
  211. currentPlaylist = playlist;
  212. }
  213. // set the number of tracks in the current playlist
  214. public void setCurrentPlaylistTracksNum(int value) {
  215. currentPlaylistTracksNum = value;
  216. }
  217. public void setCurrentPlaylistIndex(int value) {
  218. currentPlaylistIndex = value;
  219. }
  220. public boolean isRemote() {
  221. return remote;
  222. }
  223. public void setRemote(boolean remote) {
  224. this.remote = remote;
  225. }
  226. public boolean setCurrentTimeSecond(double value) {
  227. if (value == currentTimeSecond)
  228. return false;
  229. currentTimeSecond = value;
  230. return true;
  231. }
  232. public int getTrackElapsed() {
  233. double now = SystemClock.elapsedRealtime() / 1000.0;
  234. double trackCorrection = rate * (now - statusSeen);
  235. return (int) (trackCorrection <= 0 ? currentTimeSecond : currentTimeSecond + trackCorrection);
  236. }
  237. public int getCurrentSongDuration() {
  238. return currentSongDuration;
  239. }
  240. public boolean setCurrentSongDuration(int value) {
  241. if (value == currentSongDuration)
  242. return false;
  243. currentSongDuration = value;
  244. return true;
  245. }
  246. public boolean isMuted() {
  247. return currentVolume < 0;
  248. }
  249. public int getCurrentVolume() {
  250. return (currentVolume == 101 ? 0: Math.abs(currentVolume));
  251. }
  252. public boolean setCurrentVolume(int value) {
  253. if (value == currentVolume)
  254. return false;
  255. int current = currentVolume;
  256. currentVolume = value;
  257. return (current != 101); // Do not report a change if previous volume was unknown
  258. }
  259. public int getSleepDuration() {
  260. return sleepDuration;
  261. }
  262. public boolean setSleepDuration(int sleepDuration) {
  263. if (sleepDuration == this.sleepDuration)
  264. return false;
  265. this.sleepDuration = sleepDuration;
  266. return true;
  267. }
  268. /** @return seconds left until the player sleeps. */
  269. public double getSleep() {
  270. return sleep;
  271. }
  272. /**
  273. *
  274. * @param sleep seconds left until the player sleeps.
  275. * @return True if the sleep value was changed, false otherwise.
  276. */
  277. public boolean setSleep(double sleep) {
  278. if (sleep == this.sleep)
  279. return false;
  280. this.sleep = sleep;
  281. return true;
  282. }
  283. public boolean setSyncMaster(@Nullable String syncMaster) {
  284. if (syncMaster == null && mSyncMaster == null)
  285. return false;
  286. if (syncMaster != null) {
  287. if (syncMaster.equals(mSyncMaster))
  288. return false;
  289. }
  290. mSyncMaster = syncMaster;
  291. return true;
  292. }
  293. @Nullable
  294. public String getSyncMaster() {
  295. return mSyncMaster;
  296. }
  297. public boolean setSyncSlaves(@NonNull List<String> syncSlaves) {
  298. if (syncSlaves.equals(mSyncSlaves))
  299. return false;
  300. mSyncSlaves = Collections.unmodifiableList(syncSlaves);
  301. return true;
  302. }
  303. public List<String> getSyncSlaves() {
  304. return mSyncSlaves;
  305. }
  306. public PlayerSubscriptionType getSubscriptionType() {
  307. return mPlayerSubscriptionType;
  308. }
  309. public void setSubscriptionType(PlayerSubscriptionType type) {
  310. mPlayerSubscriptionType = type;
  311. }
  312. public boolean isRandomPlaying() {
  313. return mPlayR;
  314. }
  315. private static final String TAG = "PlayerState";
  316. public void setRandomPlaying(boolean b) {
  317. mPlayR = b;
  318. }
  319. @StringDef({PLAY_STATE_PLAY, PLAY_STATE_PAUSE, PLAY_STATE_STOP})
  320. @Retention(RetentionPolicy.SOURCE)
  321. public @interface PlayState {}
  322. public static final String PLAY_STATE_PLAY = "play";
  323. public static final String PLAY_STATE_PAUSE = "pause";
  324. public static final String PLAY_STATE_STOP = "stop";
  325. @Override
  326. public String toString() {
  327. return "PlayerState{" +
  328. "poweredOn=" + poweredOn +
  329. ", playStatus='" + playStatus + '\'' +
  330. ", shuffleStatus=" + shuffleStatus +
  331. ", repeatStatus=" + repeatStatus +
  332. ", currentSong=" + currentSong +
  333. ", currentPlaylist='" + currentPlaylist + '\'' +
  334. ", currentPlaylistIndex=" + currentPlaylistIndex +
  335. ", currentTimeSecond=" + currentTimeSecond +
  336. ", currentSongDuration=" + currentSongDuration +
  337. ", currentVolume=" + currentVolume +
  338. ", sleepDuration=" + sleepDuration +
  339. ", sleep=" + sleep +
  340. ", mSyncMaster='" + mSyncMaster + '\'' +
  341. ", mSyncSlaves=" + mSyncSlaves +
  342. ", mPlayerSubscriptionType='" + mPlayerSubscriptionType + '\'' +
  343. ", mPlayR='" + mPlayR + '\'' +
  344. '}';
  345. }
  346. public enum PlayerSubscriptionType {
  347. NOTIFY_NONE("-"),
  348. NOTIFY_ON_CHANGE("0");
  349. private final String status;
  350. PlayerSubscriptionType(String status) {
  351. this.status = status;
  352. }
  353. public String getStatus() {
  354. return status;
  355. }
  356. }
  357. public enum ShuffleStatus implements EnumWithId {
  358. SHUFFLE_OFF(0, R.drawable.btn_shuffle),
  359. SHUFFLE_SONG(1, R.drawable.btn_shuffle_song),
  360. SHUFFLE_ALBUM(2, R.drawable.btn_shuffle_album);
  361. private final int id;
  362. private final int icon;
  363. private static final EnumIdLookup<ShuffleStatus> lookup = new EnumIdLookup<>(
  364. ShuffleStatus.class);
  365. ShuffleStatus(int id, int icon) {
  366. this.id = id;
  367. this.icon = icon;
  368. }
  369. @Override
  370. public int getId() {
  371. return id;
  372. }
  373. @DrawableRes
  374. public int getIcon() {
  375. return icon;
  376. }
  377. public static ShuffleStatus valueOf(int id) {
  378. return lookup.get(id);
  379. }
  380. }
  381. public enum RepeatStatus implements EnumWithId {
  382. REPEAT_OFF(0, R.drawable.btn_repeat),
  383. REPEAT_ONE(1, R.drawable.btn_repeat_one),
  384. REPEAT_ALL(2, R.drawable.btn_repeat_all);
  385. private final int id;
  386. private final int icon;
  387. private static final EnumIdLookup<RepeatStatus> lookup = new EnumIdLookup<>(
  388. RepeatStatus.class);
  389. RepeatStatus(int id, int icon) {
  390. this.id = id;
  391. this.icon = icon;
  392. }
  393. @Override
  394. public int getId() {
  395. return id;
  396. }
  397. public int getIcon() {
  398. return icon;
  399. }
  400. public static RepeatStatus valueOf(int id) {
  401. return lookup.get(id);
  402. }
  403. }
  404. }