Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BackgroundMediaManagerCommunicator {
- private Messenger service = null;
- private boolean isBound = false;
- private Context context;
- final Messenger messenger = new Messenger(new IncomingHandler());
- class IncomingHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case PLAYLIST:
- playlist = playlist.getClass().cast(msg.obj);
- break;
- default:
- super.handleMessage(msg);
- }
- }
- }
- private ServiceConnection connection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className, IBinder binder) {
- service = new Messenger(binder);
- android.util.Log.v(TAG, "BackgroundMediaManagerCommunicator: Attached service");
- isBound = true;
- }
- @Override
- public void onServiceDisconnected(ComponentName className) {
- isBound = false;
- service = null;
- android.util.Log.d(TAG, "BackgroundMediaManagerCommunicator: Unexpected disconnection, Trying to reconnect");
- }
- };
- public BackgroundMediaManagerCommunicator(Context context)
- {
- this.context = context;
- }
- public boolean connect()
- {
- return context.getApplicationContext().bindService(new Intent(context.getApplicationContext(), BackgroundMediaManager.class), connection, Context.BIND_AUTO_CREATE);
- }
- public void disconnect() {
- if (isBound) {
- context.unbindService(connection);
- isBound = false;
- }
- }
- public boolean ready()
- {
- return isBound;
- }
- public class BackgroundMediaManager extends Service {
- final Messenger messenger = new Messenger(new IncomingHandler());
- class IncomingHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case PLAY:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received Play");
- play(playlist.get(msg.arg1));
- idPlaying = msg.arg1;
- break;
- case SET_PLAYLIST:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received Set playlist");
- playlist = playlist.getClass().cast(msg.obj);
- break;
- case GET_PLAYLIST:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received Get playlist");
- try {
- msg.replyTo.send(Message.obtain(null, BackgroundMediaManagerCommunicator.PLAYLIST, (Object)playlist));
- } catch (RemoteException e)
- {
- android.util.Log.d(TAG, "BackgroundMediaManager: severe case of ded client");
- }
- break;
- case SET_VIEW:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received set view");
- setDisplay((SurfaceHolder)msg.obj);
- break;
- case PAUSE:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received pause");
- if (mediaPlayer.isPlaying())
- mediaPlayer.pause();
- else
- mediaPlayer.start();
- break;
- case NEXT:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received next");
- move(1);
- break;
- case PREVIOUS:
- android.util.Log.v(TAG, "BackgroundMediaManager: Received previous");
- move(-1);
- break;
- default:
- super.handleMessage(msg);
- }
- }
- };
- public BackgroundMediaManager()
- {
- super();
- }
- @Override
- public IBinder onBind(Intent intent) {
- android.util.Log.v(TAG, "BackgroundMediaManager Bound");
- return messenger.getBinder();
- }
- @Override
- public void onCreate() {
- startCommand();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- startCommand();
- return START_STICKY;
- }
- private void startCommand()
- {
- android.util.Log.v(TAG, "BackgroundMediaManager created");
- mediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChanged);
- }
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- }
- if (!mediaManagerCommunicator.connect())
- startService(new android.content.Intent(this, BackgroundMediaManager.class));
- while (!mediaManagerCommunicator.connect()) { try { Thread.sleep(100); } catch (Exception e){} };
- outOfCreateReadying.postDelayed(new Runnable() {
- @Override
- public void run() {
- while (!mediaManagerCommunicator.ready()) { try { Thread.sleep(100); } catch (Exception e){} };
- }
- }, 100);
- <service
- android:enabled="true"
- android:name="vdragon.epiplayer.BackgroundMediaManager"
- android:process=":remote">
- </service>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement