Fixed bad behaviours when an error occurrs during audio playback
[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.datamodel.OCFile;
23 import com.owncloud.android.media.MediaService.State;
24
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;
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 //Log.e(TAG, TAG + " - canPause -> true");
70 return true;
71 }
72
73 @Override
74 public boolean canSeekBackward() {
75 //Log.e(TAG, TAG + " - canSeekBackward -> true");
76 return true;
77 }
78
79 @Override
80 public boolean canSeekForward() {
81 //Log.e(TAG, TAG + " - canSeekForward -> true");
82 return true;
83 }
84
85 @Override
86 public int getBufferPercentage() {
87 MediaPlayer currentPlayer = mService.getPlayer();
88 if (currentPlayer != null) {
89 //Log.e(TAG, TAG + " - getBufferPercentage -> 100");
90 return 100;
91 // TODO update for streamed playback; add OnBufferUpdateListener in MediaService
92 } else {
93 //Log.e(TAG, TAG + " - getBufferPercentage -> 0");
94 return 0;
95 }
96 }
97
98 @Override
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);
104 return pos;
105 } else {
106 //Log.e(TAG, TAG + " - getCurrentPosition -> 0");
107 return 0;
108 }
109 }
110
111 @Override
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);
117 return dur;
118 } else {
119 //Log.e(TAG, TAG + " - getDuration -> 0");
120 return 0;
121 }
122 }
123
124
125 /**
126 * Reports if the MediaService is playing a file or not.
127 *
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.
131 */
132 @Override
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);
137 }
138
139
140 @Override
141 public void pause() {
142 Log.d(TAG, "Pausing through binder...");
143 mService.processPauseRequest();
144 }
145
146 @Override
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);
153 }
154 }
155
156 @Override
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
160 }
161
162
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);
170 }
171
172
173 public void registerMediaController(MediaController mediaController) {
174 mService.setMediaContoller(mediaController);
175 }
176
177 public void unregisterMediaController(MediaController mediaController) {
178 if (mService.getMediaController() == mediaController) {
179 mService.setMediaContoller(null);
180 }
181
182 }
183
184 public boolean isInPlaybackState() {
185 MediaService.State currentState = mService.getState();
186 return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
187 }
188
189 }
190
191