[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / media / MediaServiceBinder.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.media;
22
23
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.lib.common.utils.Log_OC;
26 import com.owncloud.android.media.MediaService.State;
27
28 import android.accounts.Account;
29 import android.content.Intent;
30 import android.media.MediaPlayer;
31 import android.os.Binder;
32 import android.widget.MediaController;
33
34
35 /**
36 * Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
37 *
38 * Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if
39 * an {@link OCFile} instance is handled by the MediaService.
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 @Override
182 public int getAudioSessionId() {
183 return 1; // not really used
184 }
185
186 }
187
188