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