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