summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/service/PlayerWidgetService.java
blob: 2d638823d44739577f93c80b5ae8c56b54dafc8c (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
package de.danoeh.antennapod.service;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.receiver.PlayerWidget;
import de.danoeh.antennapod.util.Converter;

/** Updates the state of the player widget */
public class PlayerWidgetService extends Service {
	private static final String TAG = "PlayerWidgetService";

	private PlaybackService playbackService;
	/** True while service is updating the widget */
	private boolean isUpdating;

	public PlayerWidgetService() {
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (AppConfig.DEBUG)
			Log.d(TAG, "Service created");
		isUpdating = false;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (AppConfig.DEBUG)
			Log.d(TAG, "Service is about to be destroyed");
		try {
			unbindService(mConnection);
		} catch (IllegalArgumentException e) {
			Log.w(TAG, "IllegalArgumentException when trying to unbind service");
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (!isUpdating) {
			isUpdating = true;
			if (playbackService == null && PlaybackService.isRunning) {
				bindService(new Intent(this, PlaybackService.class),
						mConnection, 0);
			} else {
				updateViews();
				isUpdating = false;
			}
		} else {
			if (AppConfig.DEBUG)
				Log.d(TAG,
						"Service was called while updating. Ignoring update request");
		}
		return Service.START_NOT_STICKY;
	}

	private void updateViews() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Updating widget views");
		ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		RemoteViews views = new RemoteViews(getPackageName(),
				R.layout.player_widget);
		PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0,
				PlaybackService.getPlayerActivityIntent(this), 0);

		views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
		if (playbackService != null) {
			FeedMedia media = playbackService.getMedia();
			MediaPlayer player = playbackService.getPlayer();
			PlayerStatus status = playbackService.getStatus();

			views.setTextViewText(R.id.txtvTitle, media.getItem().getTitle());

			if (status == PlayerStatus.PLAYING) {
				views.setTextViewText(R.id.txtvProgress,
						getProgressString(player));
				views.setImageViewResource(R.id.butPlay, R.drawable.av_pause);
			} else {
				views.setImageViewResource(R.id.butPlay, R.drawable.av_play);
			}
			views.setOnClickPendingIntent(R.id.butPlay,
					createMediaButtonIntent());
		} else {
			if (AppConfig.DEBUG)
				Log.d(TAG, "No media playing. Displaying defaultt views");
			views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
			views.setTextViewText(R.id.txtvTitle,
					this.getString(R.string.no_media_playing_label));
			views.setImageViewResource(R.id.butPlay, R.drawable.av_play);

		}

		manager.updateAppWidget(playerWidget, views);
	}

	/** Creates an intent which fakes a mediabutton press */
	private PendingIntent createMediaButtonIntent() {
		KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,
				KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
		Intent startingIntent = new Intent(
				MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER);
		startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);

		return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
	}

	private String getProgressString(MediaPlayer player) {

		return Converter.getDurationStringLong(player.getCurrentPosition())
				+ " / " + Converter.getDurationStringLong(player.getDuration());
	}

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Connection to service established");
			playbackService = ((PlaybackService.LocalBinder) service)
					.getService();
			updateViews();
			isUpdating = false;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			playbackService = null;
			if (AppConfig.DEBUG)
				Log.d(TAG, "Disconnected from service");
		}

	};

}