Merge pull request #1432 from TomHennen/widget

Widget improvements
This commit is contained in:
Tom Hennen 2015-11-29 14:11:54 -05:00
commit 7067088cba
4 changed files with 235 additions and 173 deletions

View File

@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import org.apache.commons.lang3.StringUtils;
@ -14,36 +15,66 @@ import de.danoeh.antennapod.service.PlayerWidgetService;
public class PlayerWidget extends AppWidgetProvider {
private static final String TAG = "PlayerWidget";
private static final String PREFS_NAME = "PlayerWidgetPrefs";
private static final String KEY_ENABLED = "WidgetEnabled";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
super.onReceive(context, intent);
// don't do anything if we're not enabled
if (!isEnabled(context)) {
return;
}
// these come from the PlaybackService when things should get updated
if (StringUtils.equals(intent.getAction(), PlaybackService.FORCE_WIDGET_UPDATE)) {
startUpdate(context);
} else if (StringUtils.equals(intent.getAction(), PlaybackService.STOP_WIDGET_UPDATE)) {
stopUpdate(context);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
if (BuildConfig.DEBUG)
Log.d(TAG, "Widget enabled");
setEnabled(context, true);
startUpdate(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + appWidgetIds + "]");
startUpdate(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d(TAG, "Widet disabled");
setEnabled(context, false);
stopUpdate(context);
}
private void startUpdate(Context context) {
Log.d(TAG, "startUpdate() called with: " + "context = [" + context + "]");
context.startService(new Intent(context, PlayerWidgetService.class));
}
private void stopUpdate(Context context) {
Log.d(TAG, "stopUpdate() called with: " + "context = [" + context + "]");
context.stopService(new Intent(context, PlayerWidgetService.class));
}
private boolean isEnabled(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_ENABLED, false);
}
private void setEnabled(Context context, boolean enabled) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(KEY_ENABLED, enabled).apply();
}
}

View File

@ -14,6 +14,7 @@ import android.view.View;
import android.widget.RemoteViews;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
@ -22,18 +23,25 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.receiver.PlayerWidget;
/** Updates the state of the player widget */
/**
* Updates the state of the player widget
*/
public class PlayerWidgetService extends Service {
private static final String TAG = "PlayerWidgetService";
private PlaybackService playbackService;
/** Controls write access to playbackservice reference */
/**
* Controls write access to playbackservice reference
*/
private Object psLock;
/** True while service is updating the widget */
/**
* True while service is updating the widget
*/
private volatile boolean isUpdating;
public PlayerWidgetService() {
@ -97,9 +105,6 @@ public class PlayerWidgetService extends Service {
}
private void updateViews() {
if (playbackService == null) {
return;
}
isUpdating = true;
ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
@ -109,15 +114,23 @@ public class PlayerWidgetService extends Service {
PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0,
PlaybackService.getPlayerActivityIntent(this), 0);
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
Intent startApp = new Intent(getBaseContext(), MainActivity.class);
startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);
boolean nothingPlaying = false;
if (playbackService != null) {
final Playable media = playbackService.getPlayable();
if (playbackService != null && media != null) {
if (media != null) {
PlayerStatus status = playbackService.getStatus();
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle());
String progressString = getProgressString(media);
if (progressString != null) {
views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
views.setTextViewText(R.id.txtvProgress, progressString);
}
@ -135,18 +148,29 @@ public class PlayerWidgetService extends Service {
views.setOnClickPendingIntent(R.id.butPlay,
createMediaButtonIntent());
} else {
nothingPlaying = true;
}
} else {
nothingPlaying = true;
}
if (nothingPlaying) {
// start the app if they click anything
views.setOnClickPendingIntent(R.id.layout_left, startAppPending);
views.setOnClickPendingIntent(R.id.butPlay, startAppPending);
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.ic_play_arrow_white_24dp);
}
manager.updateAppWidget(playerWidget, views);
isUpdating = false;
}
/** Creates an intent which fakes a mediabutton press */
/**
* Creates an intent which fakes a mediabutton press
*/
private PendingIntent createMediaButtonIntent() {
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);

View File

@ -1,4 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:resizeMode="none" android:initialLayout="@layout/player_widget" android:minHeight="40dp" android:minWidth="250dp">
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:resizeMode="horizontal"
android:initialLayout="@layout/player_widget"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/ic_widget_preview"
android:minHeight="40dp"
android:minWidth="250dp"
android:minResizeWidth="40dp">
</appwidget-provider>

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB