summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/activity/MediaplayerActivity.java
blob: 162615d97545f9cbbb000469e053cfe92e406cd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package de.podfetcher.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

import de.podfetcher.R;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.service.PlayerStatus;
import de.podfetcher.util.Converter;

public class MediaplayerActivity extends SherlockActivity {
	private final String TAG = "MediaplayerActivity";
	
	private PlaybackService playbackService;
	private MediaPositionObserver positionObserver;
	
	private FeedMedia media;
	private PlayerStatus status;
	
	private boolean guiSetup;
	
	
	// Widgets
	private ImageView imgvCover;
	private TextView txtvStatus;
	private TextView txtvPosition;
	private TextView txtvLength;
	private SeekBar sbPosition;
	private ImageButton butPlay;
	private ImageButton butRev;
	private ImageButton butFF;
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "Activity stopped");
		unregisterReceiver(statusUpdate);
		unbindService(mConnection);
		if (positionObserver != null) {
			positionObserver.cancel(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.mediaplayer_activity);
		
		guiSetup = false;
		setupGUI();
		if(!bindService(new Intent(this, PlaybackService.class), mConnection, 0)) {
			status = PlayerStatus.STOPPED;
			handleStatus();
		}
		IntentFilter filter = new IntentFilter(PlaybackService.ACTION_PLAYER_STATUS_CHANGED);
		registerReceiver(statusUpdate, filter);
	}
	
	private void handleStatus() {
		switch (status) {
			
		case ERROR:
			setStatusMsg(R.string.player_error_msg, View.VISIBLE);
			handleError();
			break;
		case PAUSED:
			setStatusMsg(R.string.player_paused_msg, View.VISIBLE);
			loadMediaInfo();
			break;
		case PLAYING:
			setStatusMsg(0, View.INVISIBLE);
			loadMediaInfo();
			setupPositionObserver();
			break;
		case PREPARING:
			setStatusMsg(R.string.player_preparing_msg, View.VISIBLE);
			break;
		}
	}
	
	private void setStatusMsg(int resId, int visibility) {
		if(visibility == View.VISIBLE) {
			txtvStatus.setText(resId);
		}
		txtvStatus.setVisibility(visibility);
	}
	
	private void setupPositionObserver() {
		if (positionObserver == null) {
			positionObserver = new MediaPositionObserver() {

				@Override
				protected void onProgressUpdate(Long... values) {
					super.onProgressUpdate(values);
					txtvPosition.setText(
							Integer.toString(playbackService.getPlayer().getCurrentPosition()));
				}
				
			};
			positionObserver.execute(playbackService);
		}
	}
	
	private void loadMediaInfo() {
		if (media != null) {
			MediaPlayer player = playbackService.getPlayer();
			
			getSupportActionBar().setTitle(media.getItem().getTitle());
			getSupportActionBar().setSubtitle(
					media.getItem().getFeed().getTitle());
			
			imgvCover.setImageBitmap(
					media.getItem().getFeed().getImage().getImageBitmap());
			
			txtvPosition.setText(Integer.toString(player.getCurrentPosition()));
			txtvLength.setText(Integer.toString(player.getDuration()));
		}
	}
	
	private void setupGUI() {
		imgvCover = (ImageView) findViewById(R.id.imgvCover);
		txtvPosition = (TextView) findViewById(R.id.txtvPosition);
		txtvLength = (TextView) findViewById(R.id.txtvLength);
		txtvStatus = (TextView) findViewById(R.id.txtvStatus);
		sbPosition = (SeekBar) findViewById(R.id.sbPosition);
		butPlay = (ImageButton) findViewById(R.id.butPlay);
		butRev = (ImageButton) findViewById(R.id.butRev);
		butFF = (ImageButton) findViewById(R.id.butFF);
		
		this.guiSetup = true;
	}
	
	
	private void handleError() {
		// TODO implement
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			playbackService = ((PlaybackService.LocalBinder)service).getService();
			status = playbackService.getStatus();
			media = playbackService.getMedia();
			handleStatus();
			Log.d(TAG, "Connection to Service established");
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			playbackService = null;
			Log.d(TAG, "Disconnected from Service");
			
		}
	};
	
	private BroadcastReceiver statusUpdate = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received statusUpdate Intent.");
			status = playbackService.getStatus();
		}
	};
	
	
	public class MediaPositionObserver extends AsyncTask<PlaybackService, Long, Long> {
		
		private static final int WAITING_INTERVALL = 1000;
		private long position;
		private long length;
		private PlaybackService service;
		
		@Override
		protected void onCancelled(Long result) {
			Log.d(TAG, "Task was cancelled");
		}
				
		protected Long doInBackground(PlaybackService... services) {
			Log.d(TAG, "Background Task started");
			service = services[0];
			getProgress();
			while(!isCancelled()) {
				try {
					Thread.sleep(WAITING_INTERVALL);
				} catch(InterruptedException e) {
					Log.d(TAG, "Thread was interrupted while waiting");
				}
				
				getProgress();
				publishProgress(position);
			}
			Log.d(TAG, "Background Task finished");
			return Long.valueOf(position);
		}
		
		private void getProgress() {
			FeedMedia media = service.getMedia();
			position = media.getPosition();
			length = media.getDuration();
		}
	}
	
	
	
}