2  *   ownCloud Android client application 
   4  *   @author David A. Velasco 
   5  *   Copyright (C) 2015 ownCloud Inc. 
   7  *   This program is free software: you can redistribute it and/or modify 
   8  *   it under the terms of the GNU General Public License version 2, 
   9  *   as published by the Free Software Foundation. 
  11  *   This program is distributed in the hope that it will be useful, 
  12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14  *   GNU General Public License for more details. 
  16  *   You should have received a copy of the GNU General Public License 
  17  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  21 package com
.owncloud
.android
.media
; 
  24 import com
.owncloud
.android
.datamodel
.OCFile
; 
  25 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  26 import com
.owncloud
.android
.media
.MediaService
.State
; 
  28 import android
.accounts
.Account
; 
  29 import android
.content
.Intent
; 
  30 import android
.media
.MediaPlayer
; 
  31 import android
.os
.Binder
; 
  32 import android
.widget
.MediaController
; 
  36  *  Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance. 
  38  *  Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if 
  39  *  an {@link OCFile} instance is handled by the MediaService. 
  41 public class MediaServiceBinder 
extends Binder 
implements MediaController
.MediaPlayerControl 
{ 
  43     private static final String TAG 
= MediaServiceBinder
.class.getSimpleName(); 
  45      * {@link MediaService} instance to access with the binder 
  47     private MediaService mService 
= null
; 
  52      * @param service       A {@link MediaService} instance to access with the binder  
  54     public MediaServiceBinder(MediaService service
) { 
  55         if (service 
== null
) { 
  56             throw new IllegalArgumentException("Argument 'service' can not be null"); 
  62     public boolean isPlaying(OCFile mFile
) { 
  63         return (mFile 
!= null 
&& mFile
.equals(mService
.getCurrentFile()));  
  68     public boolean canPause() { 
  73     public boolean canSeekBackward() { 
  78     public boolean canSeekForward() { 
  83     public int getBufferPercentage() { 
  84         MediaPlayer currentPlayer 
= mService
.getPlayer(); 
  85         if (currentPlayer 
!= null
) { 
  87             // TODO update for streamed playback; add OnBufferUpdateListener in MediaService 
  94     public int getCurrentPosition() { 
  95         MediaPlayer currentPlayer 
= mService
.getPlayer(); 
  96         if (currentPlayer 
!= null
) { 
  97             int pos 
= currentPlayer
.getCurrentPosition(); 
 105     public int getDuration() { 
 106         MediaPlayer currentPlayer 
= mService
.getPlayer(); 
 107         if (currentPlayer 
!= null
) { 
 108             int dur 
= currentPlayer
.getDuration(); 
 117      * Reports if the MediaService is playing a file or not. 
 119      * Considers that the file is being played when it is in preparation because the expected 
 120      * client of this method is a {@link MediaController} , and we do not want that the 'play' 
 121      * button is shown when the file is being prepared by the MediaService. 
 124     public boolean isPlaying() { 
 125         MediaService
.State currentState 
= mService
.getState(); 
 126         return (currentState 
== State
.PLAYING 
|| (currentState 
== State
.PREPARING 
&& mService
.mPlayOnPrepared
)); 
 131     public void pause() { 
 132         Log_OC
.d(TAG
, "Pausing through binder..."); 
 133         mService
.processPauseRequest(); 
 137     public void seekTo(int pos
) { 
 138         Log_OC
.d(TAG
, "Seeking " + pos 
+ " through binder..."); 
 139         MediaPlayer currentPlayer 
= mService
.getPlayer(); 
 140         MediaService
.State currentState 
= mService
.getState(); 
 141         if (currentPlayer 
!= null 
&& currentState 
!= State
.PREPARING 
&& currentState 
!= State
.STOPPED
) { 
 142             currentPlayer
.seekTo(pos
); 
 147     public void start() { 
 148         Log_OC
.d(TAG
, "Starting through binder..."); 
 149         mService
.processPlayRequest();  // this will finish the service if there is no file preloaded to play 
 152     public void start(Account account
, OCFile file
, boolean playImmediately
, int position
) { 
 153         Log_OC
.d(TAG
, "Loading and starting through binder..."); 
 154         Intent i 
= new Intent(mService
, MediaService
.class); 
 155         i
.putExtra(MediaService
.EXTRA_ACCOUNT
, account
); 
 156         i
.putExtra(MediaService
.EXTRA_FILE
, file
); 
 157         i
.putExtra(MediaService
.EXTRA_PLAY_ON_LOAD
, playImmediately
); 
 158         i
.putExtra(MediaService
.EXTRA_START_POSITION
, position
); 
 159         i
.setAction(MediaService
.ACTION_PLAY_FILE
); 
 160         mService
.startService(i
); 
 164     public void registerMediaController(MediaControlView mediaController
) { 
 165         mService
.setMediaContoller(mediaController
); 
 168     public void unregisterMediaController(MediaControlView mediaController
) { 
 169         if (mediaController 
!= null 
&& mediaController 
== mService
.getMediaController()) { 
 170             mService
.setMediaContoller(null
); 
 175     public boolean isInPlaybackState() { 
 176         MediaService
.State currentState 
= mService
.getState(); 
 177         return (currentState 
== MediaService
.State
.PLAYING 
|| currentState 
== MediaService
.State
.PAUSED
); 
 182     public int getAudioSessionId() { 
 183         return 1; // not really used