Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. public class BackgroundMediaManagerCommunicator {
  2. private Messenger service = null;
  3. private boolean isBound = false;
  4. private Context context;
  5. final Messenger messenger = new Messenger(new IncomingHandler());
  6. class IncomingHandler extends Handler {
  7. @Override
  8. public void handleMessage(Message msg) {
  9. switch (msg.what) {
  10. case PLAYLIST:
  11. playlist = playlist.getClass().cast(msg.obj);
  12. break;
  13. default:
  14. super.handleMessage(msg);
  15. }
  16. }
  17. }
  18.  
  19. private ServiceConnection connection = new ServiceConnection() {
  20. @Override
  21. public void onServiceConnected(ComponentName className, IBinder binder) {
  22. service = new Messenger(binder);
  23. android.util.Log.v(TAG, "BackgroundMediaManagerCommunicator: Attached service");
  24. isBound = true;
  25. }
  26. @Override
  27. public void onServiceDisconnected(ComponentName className) {
  28. isBound = false;
  29. service = null;
  30. android.util.Log.d(TAG, "BackgroundMediaManagerCommunicator: Unexpected disconnection, Trying to reconnect");
  31. }
  32. };
  33.  
  34. public BackgroundMediaManagerCommunicator(Context context)
  35. {
  36. this.context = context;
  37. }
  38.  
  39. public boolean connect()
  40. {
  41. return context.getApplicationContext().bindService(new Intent(context.getApplicationContext(), BackgroundMediaManager.class), connection, Context.BIND_AUTO_CREATE);
  42. }
  43.  
  44. public void disconnect() {
  45. if (isBound) {
  46. context.unbindService(connection);
  47. isBound = false;
  48. }
  49. }
  50.  
  51. public boolean ready()
  52. {
  53. return isBound;
  54. }
  55.  
  56. public class BackgroundMediaManager extends Service {
  57. final Messenger messenger = new Messenger(new IncomingHandler());
  58. class IncomingHandler extends Handler {
  59. @Override
  60. public void handleMessage(Message msg) {
  61. switch (msg.what) {
  62. case PLAY:
  63. android.util.Log.v(TAG, "BackgroundMediaManager: Received Play");
  64. play(playlist.get(msg.arg1));
  65. idPlaying = msg.arg1;
  66. break;
  67. case SET_PLAYLIST:
  68. android.util.Log.v(TAG, "BackgroundMediaManager: Received Set playlist");
  69. playlist = playlist.getClass().cast(msg.obj);
  70. break;
  71. case GET_PLAYLIST:
  72. android.util.Log.v(TAG, "BackgroundMediaManager: Received Get playlist");
  73. try {
  74. msg.replyTo.send(Message.obtain(null, BackgroundMediaManagerCommunicator.PLAYLIST, (Object)playlist));
  75. } catch (RemoteException e)
  76. {
  77. android.util.Log.d(TAG, "BackgroundMediaManager: severe case of ded client");
  78. }
  79. break;
  80. case SET_VIEW:
  81. android.util.Log.v(TAG, "BackgroundMediaManager: Received set view");
  82. setDisplay((SurfaceHolder)msg.obj);
  83. break;
  84. case PAUSE:
  85. android.util.Log.v(TAG, "BackgroundMediaManager: Received pause");
  86. if (mediaPlayer.isPlaying())
  87. mediaPlayer.pause();
  88. else
  89. mediaPlayer.start();
  90. break;
  91. case NEXT:
  92. android.util.Log.v(TAG, "BackgroundMediaManager: Received next");
  93. move(1);
  94. break;
  95. case PREVIOUS:
  96. android.util.Log.v(TAG, "BackgroundMediaManager: Received previous");
  97. move(-1);
  98. break;
  99. default:
  100. super.handleMessage(msg);
  101. }
  102. }
  103. };
  104.  
  105. public BackgroundMediaManager()
  106. {
  107. super();
  108. }
  109.  
  110. @Override
  111. public IBinder onBind(Intent intent) {
  112. android.util.Log.v(TAG, "BackgroundMediaManager Bound");
  113. return messenger.getBinder();
  114. }
  115. @Override
  116. public void onCreate() {
  117. startCommand();
  118. }
  119. @Override
  120. public int onStartCommand(Intent intent, int flags, int startId) {
  121. startCommand();
  122. return START_STICKY;
  123. }
  124. private void startCommand()
  125. {
  126. android.util.Log.v(TAG, "BackgroundMediaManager created");
  127. mediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChanged);
  128. }
  129. @Override
  130. public void onDestroy()
  131. {
  132. super.onDestroy();
  133. }
  134.  
  135. if (!mediaManagerCommunicator.connect())
  136. startService(new android.content.Intent(this, BackgroundMediaManager.class));
  137. while (!mediaManagerCommunicator.connect()) { try { Thread.sleep(100); } catch (Exception e){} };
  138. outOfCreateReadying.postDelayed(new Runnable() {
  139. @Override
  140. public void run() {
  141. while (!mediaManagerCommunicator.ready()) { try { Thread.sleep(100); } catch (Exception e){} };
  142. }
  143.  
  144. }, 100);
  145.  
  146. <service
  147. android:enabled="true"
  148. android:name="vdragon.epiplayer.BackgroundMediaManager"
  149. android:process=":remote">
  150. </service>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement