summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java
blob: bc4f7bdecee92d2ef20f755890e08d62b8e59a1e (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.danoeh.antennapod.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.Playable;
import de.danoeh.antennapod.util.PlaybackController;

/**
 * Fragment which is supposed to be displayed outside of the MediaplayerActivity
 * if the PlaybackService is running
 */
public class ExternalPlayerFragment extends SherlockFragment {
	private static final String TAG = "ExternalPlayerFragment";

	private ViewGroup fragmentLayout;
	private ImageView imgvCover;
	private ViewGroup layoutInfo;
	private TextView txtvTitle;
	private TextView txtvPosition;
	private TextView txtvStatus;
	private ImageButton butPlay;

	private PlaybackController controller;

	public ExternalPlayerFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.external_player_fragment,
				container, false);
		fragmentLayout = (ViewGroup) root.findViewById(R.id.fragmentLayout);
		imgvCover = (ImageView) root.findViewById(R.id.imgvCover);
		layoutInfo = (ViewGroup) root.findViewById(R.id.layoutInfo);
		txtvTitle = (TextView) root.findViewById(R.id.txtvTitle);
		txtvPosition = (TextView) root.findViewById(R.id.txtvPosition);
		butPlay = (ImageButton) root.findViewById(R.id.butPlay);
		txtvStatus = (TextView) root.findViewById(R.id.txtvStatus);

		layoutInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "layoutInfo was clicked");

				if (controller.getMedia() != null) {
					startActivity(PlaybackService.getPlayerActivityIntent(
							getActivity(), controller.getMedia()));
				}
			}
		});
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		controller = setupPlaybackController();
		butPlay.setOnClickListener(controller.newOnPlayButtonClickListener());
	}

	private PlaybackController setupPlaybackController() {
		return new PlaybackController(getActivity(), true) {

			@Override
			public void setupGUI() {
			}

			@Override
			public void onPositionObserverUpdate() {
				int duration = controller.getDuration();
				int position = controller.getPosition();
				if (duration != PlaybackController.INVALID_TIME
						&& position != PlaybackController.INVALID_TIME) {
					txtvPosition.setText(getPositionString(position, duration));
				}
			}

			@Override
			public void onReloadNotification(int code) {
			}

			@Override
			public void onBufferStart() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBufferEnd() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBufferUpdate(float progress) {
			}

			@Override
			public void onSleepTimerUpdate() {
			}

			@Override
			public void handleError(int code) {
			}

			@Override
			public ImageButton getPlayButton() {
				return butPlay;
			}

			@Override
			public void postStatusMsg(int msg) {
				txtvStatus.setText(msg);
			}

			@Override
			public void clearStatusMsg() {
				txtvStatus.setText("");
			}

			@Override
			public void loadMediaInfo() {
				ExternalPlayerFragment.this.loadMediaInfo();
			}

			@Override
			public void onAwaitingVideoSurface() {
			}

			@Override
			public void onServiceQueried() {
			}

			@Override
			public void onShutdownNotification() {
				if (fragmentLayout != null) {
					fragmentLayout.setVisibility(View.GONE);
				}
				controller = setupPlaybackController();
				if (butPlay != null) {
					butPlay.setOnClickListener(controller
							.newOnPlayButtonClickListener());
				}

			}
		};
	}

	@Override
	public void onResume() {
		super.onResume();
		controller.init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (AppConfig.DEBUG)
			Log.d(TAG, "Fragment is about to be destroyed");
		if (controller != null) {
			controller.release();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (controller != null) {
			controller.pause();
		}
	}

	private void loadMediaInfo() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Loading media info");
		if (controller.serviceAvailable()) {
			Playable media = controller.getMedia();
			if (media != null) {
				txtvTitle.setText(media.getEpisodeTitle());
				ImageLoader.getInstance().loadThumbnailBitmap(
						media.getImageFileUrl(),
						imgvCover,
						(int) getActivity().getResources().getDimension(
								R.dimen.external_player_height));

				txtvPosition.setText(getPositionString(media.getPosition(),
						media.getDuration()));
				fragmentLayout.setVisibility(View.VISIBLE);
				if (controller.isPlayingVideo()) {
					butPlay.setVisibility(View.GONE);
				} else {
					butPlay.setVisibility(View.VISIBLE);
				}
			} else {
				Log.w(TAG,
						"loadMediaInfo was called while the media object of playbackService was null!");
			}
		} else {
			Log.w(TAG,
					"loadMediaInfo was called while playbackService was null!");
		}
	}

	private String getPositionString(int position, int duration) {
		return Converter.getDurationStringLong(position) + " / "
				+ Converter.getDurationStringLong(duration);
	}
}