1 /* ownCloud Android client application
2 * Copyright (C) 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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.media
;
22 import com
.owncloud
.android
.Log_OC
;
23 import com
.owncloud
.android
.datamodel
.OCFile
;
24 import com
.owncloud
.android
.media
.MediaService
.State
;
26 import android
.accounts
.Account
;
27 import android
.content
.Intent
;
28 import android
.media
.MediaPlayer
;
29 import android
.os
.Binder
;
30 import android
.widget
.MediaController
;
34 * Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
36 * Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if
37 * an {@link OCFile} instance is handled by the MediaService.
39 * @author David A. Velasco
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
);