Merge branch 'develop' into loggingtool
[pub/Android/ownCloud.git] / src / com / owncloud / android / media / MediaService.java
index 5f4e1ac..9fd0037 100644 (file)
@@ -35,12 +35,10 @@ import android.net.wifi.WifiManager.WifiLock;
 import android.os.IBinder;
 import android.os.PowerManager;
 import android.util.Log;
-import android.widget.MediaController;
 import android.widget.Toast;
 
 import java.io.IOException;
 
-import com.owncloud.android.AccountUtils;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.ui.activity.FileDetailActivity;
@@ -62,17 +60,21 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     private static final String MY_PACKAGE = MediaService.class.getPackage() != null ? MediaService.class.getPackage().getName() : "com.owncloud.android.media";
     
     /// Intent actions that we are prepared to handle
-    public static final String ACTION_PLAY_FILE = MY_PACKAGE + ".android.media.action.PLAY_FILE";
+    public static final String ACTION_PLAY_FILE = MY_PACKAGE + ".action.PLAY_FILE";
+    public static final String ACTION_STOP_ALL = MY_PACKAGE + ".action.STOP_ALL";
 
     /// Keys to add extras to the action
     public static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
     public static final String EXTRA_ACCOUNT = MY_PACKAGE + ".extra.ACCOUNT";
+    public static String EXTRA_START_POSITION = MY_PACKAGE + ".extra.START_POSITION";
+    public static final String EXTRA_PLAY_ON_LOAD = MY_PACKAGE + ".extra.PLAY_ON_LOAD";
+
 
     /** Error code for specific messages - see regular error codes at {@link MediaPlayer} */
     public static final int OC_MEDIA_ERROR = 0;
 
     /** Time To keep the control panel visible when the user does not use it */
-    public static final int MEDIA_CONTROL_SHORT_LIFE = 5000;
+    public static final int MEDIA_CONTROL_SHORT_LIFE = 4000;
     
     /** Time To keep the control panel visible when the user does not use it */
     public static final int MEDIA_CONTROL_PERMANENT = 0;
@@ -128,11 +130,17 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /** Account holding the file being played */
     private Account mAccount;
 
+    /** Flag signaling if the audio should be played immediately when the file is prepared */ 
+    protected boolean mPlayOnPrepared;
+
+    /** Position, in miliseconds, where the audio should be started */
+    private int mStartPosition;
+    
     /** Interface to access the service through binding */
     private IBinder mBinder;
 
     /** Control panel shown to the user to control the playback, to register through binding */
-    private MediaController mMediaController;
+    private MediaControlView mMediaController;
     
 
     
@@ -231,6 +239,9 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         String action = intent.getAction();
         if (action.equals(ACTION_PLAY_FILE)) { 
             processPlayFileRequest(intent);
+            
+        } else if (action.equals(ACTION_STOP_ALL)) {
+            processStopRequest(true);
         }
 
         return START_NOT_STICKY; // don't want it to restart in case it's killed.
@@ -248,6 +259,8 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         if (mState != State.PREPARING) {
             mFile = intent.getExtras().getParcelable(EXTRA_FILE);
             mAccount = intent.getExtras().getParcelable(EXTRA_ACCOUNT);
+            mPlayOnPrepared = intent.getExtras().getBoolean(EXTRA_PLAY_ON_LOAD, false);
+            mStartPosition = intent.getExtras().getInt(EXTRA_START_POSITION, 0);
             tryToGetAudioFocus();
             playMedia();
         }
@@ -477,11 +490,16 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     
     /** Called when media player is done playing current song. */
     public void onCompletion(MediaPlayer player) {
+        Toast.makeText(this, String.format(getString(R.string.media_event_done, mFile.getFileName())), Toast.LENGTH_LONG).show();
         if (mMediaController != null) {
-            mMediaController.hide();
+            // somebody is still bound to the service
+            player.seekTo(0);
+            processPauseRequest();
+            mMediaController.updatePausePlay();
+        } else {
+            // nobody is bound
+            processStopRequest(true);
         }
-        Toast.makeText(this, String.format(getString(R.string.media_event_done, mFile.getFileName())), Toast.LENGTH_LONG).show();
-        processStopRequest(true);
         return;
     }
     
@@ -497,9 +515,14 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         if (mMediaController != null) {
             mMediaController.setEnabled(true);
         }
+        player.seekTo(mStartPosition);
         configAndStartMediaPlayer();
+        if (!mPlayOnPrepared) {
+            processPauseRequest();
+        }
+        
         if (mMediaController != null) {
-            mMediaController.show(MEDIA_CONTROL_PERMANENT);
+            mMediaController.updatePausePlay();
         }
     }
     
@@ -572,10 +595,6 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     public boolean onError(MediaPlayer mp, int what, int extra) {
         Log.e(TAG, "Error in audio playback, what = " + what + ", extra = " + extra);
         
-        if (mMediaController != null) {
-            mMediaController.hide();
-        }
-
         String message = getMessageForMediaError(this, what, extra);
         Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
         
@@ -676,15 +695,11 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     }
 
 
-    protected void setMediaContoller(MediaController mediaController) {
-        if (mMediaController != null) {
-            mMediaController.hide();
-        }
+    protected void setMediaContoller(MediaControlView mediaController) {
         mMediaController = mediaController;
-        
     }
 
-    protected MediaController getMediaController() {
+    protected MediaControlView getMediaController() {
         return mMediaController;
     }