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
.datamodel
.OCFile
;
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
.util
.Log
;
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() {
69 //Log.e(TAG, TAG + " - canPause -> true");
74 public boolean canSeekBackward() {
75 //Log.e(TAG, TAG + " - canSeekBackward -> true");
80 public boolean canSeekForward() {
81 //Log.e(TAG, TAG + " - canSeekForward -> true");
86 public int getBufferPercentage() {
87 MediaPlayer currentPlayer
= mService
.getPlayer();
88 if (currentPlayer
!= null
) {
89 //Log.e(TAG, TAG + " - getBufferPercentage -> 100");
91 // TODO update for streamed playback; add OnBufferUpdateListener in MediaService
93 //Log.e(TAG, TAG + " - getBufferPercentage -> 0");
99 public int getCurrentPosition() {
100 MediaPlayer currentPlayer
= mService
.getPlayer();
101 if (currentPlayer
!= null
) {
102 int pos
= currentPlayer
.getCurrentPosition();
103 //Log.e(TAG, TAG + " - getCurrentPosition -> " + pos);
106 //Log.e(TAG, TAG + " - getCurrentPosition -> 0");
112 public int getDuration() {
113 MediaPlayer currentPlayer
= mService
.getPlayer();
114 if (currentPlayer
!= null
) {
115 int dur
= currentPlayer
.getDuration();
116 //Log.e(TAG, TAG + " - getDuration -> " + dur);
119 //Log.e(TAG, TAG + " - getDuration -> 0");
126 * Reports if the MediaService is playing a file or not.
128 * Considers that the file is being played when it is in preparation because the expected
129 * client of this method is a {@link MediaController} , and we do not want that the 'play'
130 * button is shown when the file is being prepared by the MediaService.
133 public boolean isPlaying() {
134 MediaService
.State currentState
= mService
.getState();
135 //Log.e(TAG, TAG + " - isPlaying -> " + (currentState == State.PLAYING || currentState == State.PREPARING));
136 return (currentState
== State
.PLAYING
|| currentState
== State
.PREPARING
);
141 public void pause() {
142 Log
.d(TAG
, "Pausing through binder...");
143 mService
.processPauseRequest();
147 public void seekTo(int pos
) {
148 Log
.d(TAG
, "Seeking " + pos
+ " through binder...");
149 MediaPlayer currentPlayer
= mService
.getPlayer();
150 MediaService
.State currentState
= mService
.getState();
151 if (currentPlayer
!= null
&& currentState
!= State
.PREPARING
&& currentState
!= State
.STOPPED
) {
152 currentPlayer
.seekTo(pos
);
157 public void start() {
158 Log
.d(TAG
, "Starting through binder...");
159 mService
.processPlayRequest(); // this will finish the service if there is no file preloaded to play
163 public void start(Account account
, OCFile file
) {
164 Log
.d(TAG
, "Loading and starting through binder...");
165 Intent i
= new Intent(mService
, MediaService
.class);
166 i
.putExtra(MediaService
.EXTRA_ACCOUNT
, account
);
167 i
.putExtra(MediaService
.EXTRA_FILE
, file
);
168 i
.setAction(MediaService
.ACTION_PLAY_FILE
);
169 mService
.startService(i
);
173 public void registerMediaController(MediaController mediaController
) {
174 mService
.setMediaContoller(mediaController
);
177 public void unregisterMediaController(MediaController mediaController
) {
178 if (mService
.getMediaController() == mediaController
) {
179 mService
.setMediaContoller(null
);
184 public boolean isInPlaybackState() {
185 MediaService
.State currentState
= mService
.getState();
186 return (currentState
== MediaService
.State
.PLAYING
|| currentState
== MediaService
.State
.PAUSED
);