/MATE-20_EMUI_11.0.0/src/main/java/android/media/AsyncPlayer.java

https://github.com/dstmath/HWFramework · Java · 186 lines · 164 code · 16 blank · 6 comment · 40 complexity · 5fda861e37f8462d9e49973442f0a423 MD5 · raw file

  1. package android.media;
  2. import android.annotation.UnsupportedAppUsage;
  3. import android.content.Context;
  4. import android.media.AudioAttributes;
  5. import android.net.Uri;
  6. import android.os.PowerManager;
  7. import android.os.SystemClock;
  8. import android.util.Log;
  9. import java.util.LinkedList;
  10. public class AsyncPlayer {
  11. private static final int PLAY = 1;
  12. private static final int STOP = 2;
  13. private static final boolean mDebug = false;
  14. private final LinkedList<Command> mCmdQueue = new LinkedList<>();
  15. private MediaPlayer mPlayer;
  16. private int mState = 2;
  17. private String mTag;
  18. private Thread mThread;
  19. private PowerManager.WakeLock mWakeLock;
  20. /* access modifiers changed from: private */
  21. public static final class Command {
  22. AudioAttributes attributes;
  23. int code;
  24. Context context;
  25. boolean looping;
  26. long requestTime;
  27. Uri uri;
  28. private Command() {
  29. }
  30. public String toString() {
  31. return "{ code=" + this.code + " looping=" + this.looping + " attr=" + this.attributes + " uri=" + this.uri + " }";
  32. }
  33. }
  34. /* access modifiers changed from: private */
  35. /* access modifiers changed from: public */
  36. private void startSound(Command cmd) {
  37. try {
  38. MediaPlayer player = new MediaPlayer();
  39. player.setAudioAttributes(cmd.attributes);
  40. player.setDataSource(cmd.context, cmd.uri);
  41. player.setLooping(cmd.looping);
  42. player.prepare();
  43. player.start();
  44. if (this.mPlayer != null) {
  45. this.mPlayer.release();
  46. }
  47. this.mPlayer = player;
  48. long delay = SystemClock.uptimeMillis() - cmd.requestTime;
  49. if (delay > 1000) {
  50. String str = this.mTag;
  51. Log.w(str, "Notification sound delayed by " + delay + "msecs");
  52. }
  53. } catch (Exception e) {
  54. Log.w(this.mTag, "error loading sound", e);
  55. }
  56. }
  57. /* access modifiers changed from: private */
  58. public final class Thread extends Thread {
  59. Thread() {
  60. super("AsyncPlayer-" + AsyncPlayer.this.mTag);
  61. }
  62. @Override // java.lang.Thread, java.lang.Runnable
  63. public void run() {
  64. Command cmd;
  65. while (true) {
  66. synchronized (AsyncPlayer.this.mCmdQueue) {
  67. cmd = (Command) AsyncPlayer.this.mCmdQueue.removeFirst();
  68. }
  69. int i = cmd.code;
  70. if (i == 1) {
  71. AsyncPlayer.this.startSound(cmd);
  72. } else if (i == 2) {
  73. if (AsyncPlayer.this.mPlayer != null) {
  74. long delay = SystemClock.uptimeMillis() - cmd.requestTime;
  75. if (delay > 1000) {
  76. String str = AsyncPlayer.this.mTag;
  77. Log.w(str, "Notification stop delayed by " + delay + "msecs");
  78. }
  79. AsyncPlayer.this.mPlayer.stop();
  80. AsyncPlayer.this.mPlayer.release();
  81. AsyncPlayer.this.mPlayer = null;
  82. } else {
  83. Log.w(AsyncPlayer.this.mTag, "STOP command without a player");
  84. }
  85. }
  86. synchronized (AsyncPlayer.this.mCmdQueue) {
  87. if (AsyncPlayer.this.mCmdQueue.size() == 0) {
  88. AsyncPlayer.this.mThread = null;
  89. AsyncPlayer.this.releaseWakeLock();
  90. return;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. public AsyncPlayer(String tag) {
  97. if (tag != null) {
  98. this.mTag = tag;
  99. } else {
  100. this.mTag = "AsyncPlayer";
  101. }
  102. }
  103. public void play(Context context, Uri uri, boolean looping, int stream) {
  104. PlayerBase.deprecateStreamTypeForPlayback(stream, "AsyncPlayer", "play()");
  105. if (context != null && uri != null) {
  106. try {
  107. play(context, uri, looping, new AudioAttributes.Builder().setInternalLegacyStreamType(stream).build());
  108. } catch (IllegalArgumentException e) {
  109. Log.e(this.mTag, "Call to deprecated AsyncPlayer.play() method caused:", e);
  110. }
  111. }
  112. }
  113. public void play(Context context, Uri uri, boolean looping, AudioAttributes attributes) throws IllegalArgumentException {
  114. if (context == null || uri == null || attributes == null) {
  115. throw new IllegalArgumentException("Illegal null AsyncPlayer.play() argument");
  116. }
  117. Command cmd = new Command();
  118. cmd.requestTime = SystemClock.uptimeMillis();
  119. cmd.code = 1;
  120. cmd.context = context;
  121. cmd.uri = uri;
  122. cmd.looping = looping;
  123. cmd.attributes = attributes;
  124. synchronized (this.mCmdQueue) {
  125. enqueueLocked(cmd);
  126. this.mState = 1;
  127. }
  128. }
  129. public void stop() {
  130. synchronized (this.mCmdQueue) {
  131. if (this.mState != 2) {
  132. Command cmd = new Command();
  133. cmd.requestTime = SystemClock.uptimeMillis();
  134. cmd.code = 2;
  135. enqueueLocked(cmd);
  136. this.mState = 2;
  137. }
  138. }
  139. }
  140. private void enqueueLocked(Command cmd) {
  141. this.mCmdQueue.add(cmd);
  142. if (this.mThread == null) {
  143. acquireWakeLock();
  144. this.mThread = new Thread();
  145. this.mThread.start();
  146. }
  147. }
  148. @UnsupportedAppUsage
  149. public void setUsesWakeLock(Context context) {
  150. if (this.mWakeLock == null && this.mThread == null) {
  151. this.mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(1, this.mTag);
  152. return;
  153. }
  154. throw new RuntimeException("assertion failed mWakeLock=" + this.mWakeLock + " mThread=" + this.mThread);
  155. }
  156. private void acquireWakeLock() {
  157. PowerManager.WakeLock wakeLock = this.mWakeLock;
  158. if (wakeLock != null) {
  159. wakeLock.acquire();
  160. }
  161. }
  162. /* access modifiers changed from: private */
  163. /* access modifiers changed from: public */
  164. private void releaseWakeLock() {
  165. PowerManager.WakeLock wakeLock = this.mWakeLock;
  166. if (wakeLock != null) {
  167. wakeLock.release();
  168. }
  169. }
  170. }