PageRenderTime 72ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

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