PageRenderTime 38ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/kaaholst/android-squeezer
Java | 201 lines | 140 code | 40 blank | 21 comment | 7 complexity | caa3afc46a4b5924a22177ec7a6eeb6a MD5 | raw file
  1. /*
  2. * Copyright (c) 2011 Kurt Aaholst <kaaholst@gmail.com>
  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.SystemClock;
  19. import androidx.annotation.NonNull;
  20. import java.util.Comparator;
  21. import java.util.Map;
  22. import uk.org.ngo.squeezer.Util;
  23. import uk.org.ngo.squeezer.service.event.SongTimeChanged;
  24. public class Player extends Item implements Comparable<Player> {
  25. private String mName;
  26. private final String mIp;
  27. private final String mModel;
  28. private final boolean mCanPowerOff;
  29. private final PlayerState mPlayerState = new PlayerState();
  30. /** Is the player connected? */
  31. private boolean mConnected;
  32. @Override
  33. public int compareTo(@NonNull Player otherPlayer) {
  34. return this.mName.compareToIgnoreCase((otherPlayer).mName);
  35. }
  36. /** The types of player preferences. */
  37. public enum Pref {
  38. ALARM_DEFAULT_VOLUME("alarmDefaultVolume"),
  39. ALARM_FADE_SECONDS("alarmfadeseconds"),
  40. ALARM_SNOOZE_SECONDS("alarmSnoozeSeconds"),
  41. ALARM_TIMEOUT_SECONDS("alarmTimeoutSeconds"),
  42. ALARMS_ENABLED("alarmsEnabled"),
  43. PLAY_TRACK_ALBUM("playtrackalbum"),
  44. DEFEAT_DESTRUCTIVE_TTP("defeatDestructiveTouchToPlay"),
  45. SYNC_VOLUME("syncVolume"),
  46. SYNC_POWER("syncPower"),
  47. DIGITAL_VOLUME_CONTROL("digitalVolumeControl");
  48. private final String prefName;
  49. Pref(String prefName) {
  50. this.prefName = prefName;
  51. }
  52. public String prefName() {
  53. return prefName;
  54. }
  55. }
  56. public Player(Map<String, Object> record) {
  57. setId(getString(record, "playerid"));
  58. mIp = getString(record, "ip");
  59. mName = getString(record, "name");
  60. mModel = getString(record, "model");
  61. mCanPowerOff = getInt(record, "canpoweroff") == 1;
  62. mConnected = getInt(record, "connected") == 1;
  63. for (Player.Pref pref : Player.Pref.values()) {
  64. if (record.containsKey(pref.prefName)) {
  65. mPlayerState.prefs.put(pref, Util.getString(record, pref.prefName));
  66. }
  67. }
  68. }
  69. private Player(Parcel source) {
  70. setId(source.readString());
  71. mIp = source.readString();
  72. mName = source.readString();
  73. mModel = source.readString();
  74. mCanPowerOff = (source.readByte() == 1);
  75. mConnected = (source.readByte() == 1);
  76. }
  77. @NonNull
  78. @Override
  79. public String getName() {
  80. return mName;
  81. }
  82. public Player setName(@NonNull String name) {
  83. this.mName = name;
  84. return this;
  85. }
  86. public String getIp() {
  87. return mIp;
  88. }
  89. public String getModel() {
  90. return mModel;
  91. }
  92. public boolean isCanpoweroff() {
  93. return mCanPowerOff;
  94. }
  95. public void setConnected(boolean connected) {
  96. mConnected = connected;
  97. }
  98. public boolean getConnected() {
  99. return mConnected;
  100. }
  101. @NonNull
  102. public PlayerState getPlayerState() {
  103. return mPlayerState;
  104. }
  105. public static final Creator<Player> CREATOR = new Creator<>() {
  106. @Override
  107. public Player[] newArray(int size) {
  108. return new Player[size];
  109. }
  110. @Override
  111. public Player createFromParcel(Parcel source) {
  112. return new Player(source);
  113. }
  114. };
  115. @Override
  116. public void writeToParcel(Parcel dest, int flags) {
  117. dest.writeString(getId());
  118. dest.writeString(mIp);
  119. dest.writeString(mName);
  120. dest.writeString(mModel);
  121. dest.writeByte(mCanPowerOff ? (byte) 1 : (byte) 0);
  122. dest.writeByte(mConnected ? (byte) 1 : (byte) 0);
  123. }
  124. /**
  125. * Comparator to compare two players by ID.
  126. */
  127. public static final Comparator<Player> compareById = Comparator.comparing(Item::getId);
  128. @Override
  129. public boolean equals(Object o) {
  130. if (!super.equals(o)) {
  131. return false;
  132. }
  133. // super.equals() has already checked that o is not null and is of the same class.
  134. Player p = (Player) o;
  135. return getName().equals(p.getName());
  136. }
  137. @NonNull
  138. @Override
  139. public String toString() {
  140. return "Player{" +
  141. "mName='" + mName + '\'' +
  142. ", mIp='" + mIp + '\'' +
  143. ", mModel='" + mModel + '\'' +
  144. ", mCanPowerOff=" + mCanPowerOff +
  145. ", mPlayerState=" + mPlayerState +
  146. ", mConnected=" + mConnected +
  147. '}';
  148. }
  149. public SongTimeChanged getTrackElapsed() {
  150. return new SongTimeChanged(this, mPlayerState.getTrackElapsed(), mPlayerState.getCurrentSongDuration());
  151. }
  152. public int getSleepingIn() {
  153. double now = SystemClock.elapsedRealtime() / 1000.0;
  154. double correction = now - mPlayerState.statusSeen;
  155. double remaining = (correction <= 0 ? mPlayerState.getSleep() : mPlayerState.getSleep() - correction);
  156. return (int) remaining;
  157. }
  158. public boolean isSyncVolume() {
  159. return "1".equals(getPlayerState().prefs.get(Player.Pref.SYNC_VOLUME));
  160. }
  161. }