d2c5248456b2236b45033cf4b5d8c72a28b44362
[pub/Android/ownCloud.git] / src / com / owncloud / android / media / MediaServiceBinder.java
1 /* ownCloud Android client application
2 * Copyright (C) 2013 ownCloud Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.media;
20
21
22 import com.owncloud.android.Log_OC;
23 import com.owncloud.android.datamodel.OCFile;
24 import com.owncloud.android.media.MediaService.State;
25
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;
31
32
33 /**
34 * Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
35 *
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.
38 *
39 * @author David A. Velasco
40 */
41 public class MediaServiceBinder extends Binder implements MediaController.MediaPlayerControl {
42
43 private static final String TAG = MediaServiceBinder.class.getSimpleName();
44 /**
45 * {@link MediaService} instance to access with the binder
46 */
47 private MediaService mService = null;
48
49 /**
50 * Public constructor
51 *
52 * @param service A {@link MediaService} instance to access with the binder
53 */
54 public MediaServiceBinder(MediaService service) {
55 if (service == null) {
56 throw new IllegalArgumentException("Argument 'service' can not be null");
57 }
58 mService = service;
59 }
60
61
62 public boolean isPlaying(OCFile mFile) {
63 return (mFile != null && mFile.equals(mService.getCurrentFile()));
64 }
65
66
67 @Override
68 public boolean canPause() {
69 return true;
70 }
71
72 @Override
73 public boolean canSeekBackward() {
74 return true;
75 }
76
77 @Override
78 public boolean canSeekForward() {
79 return true;
80 }
81
82 @Override
83 public int getBufferPercentage() {
84 MediaPlayer currentPlayer = mService.getPlayer();
85 if (currentPlayer != null) {
86 return 100;
87 // TODO update for streamed playback; add OnBufferUpdateListener in MediaService
88 } else {
89 return 0;
90 }
91 }
92
93 @Override
94 public int getCurrentPosition() {
95 MediaPlayer currentPlayer = mService.getPlayer();
96 if (currentPlayer != null) {
97 int pos = currentPlayer.getCurrentPosition();
98 return pos;
99 } else {
100 return 0;
101 }
102 }
103
104 @Override
105 public int getDuration() {
106 MediaPlayer currentPlayer = mService.getPlayer();
107 if (currentPlayer != null) {
108 int dur = currentPlayer.getDuration();
109 return dur;
110 } else {
111 return 0;
112 }
113 }
114
115
116 /**
117 * Reports if the MediaService is playing a file or not.
118 *
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.
122 */
123 @Override
124 public boolean isPlaying() {
125 MediaService.State currentState = mService.getState();
126 return (currentState == State.PLAYING || (currentState == State.PREPARING && mService.mPlayOnPrepared));
127 }
128
129
130 @Override
131 public void pause() {
132 Log_OC.d(TAG, "Pausing through binder...");
133 mService.processPauseRequest();
134 }
135
136 @Override
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);
143 }
144 }
145
146 @Override
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
150 }
151
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);
161 }
162
163
164 public void registerMediaController(MediaControlView mediaController) {
165 mService.setMediaContoller(mediaController);
166 }
167
168 public void unregisterMediaController(MediaControlView mediaController) {
169 if (mediaController != null && mediaController == mService.getMediaController()) {
170 mService.setMediaContoller(null);
171 }
172
173 }
174
175 public boolean isInPlaybackState() {
176 MediaService.State currentState = mService.getState();
177 return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
178 }
179
180 }
181
182