Commit 3c0f10d4 authored by Geoffrey Métais's avatar Geoffrey Métais

Send widget updates only if a widget is active

And restrict updates during playback
parent ae7e29b7
......@@ -24,6 +24,7 @@ import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
......@@ -95,6 +96,8 @@ import org.videolan.vlc.util.VLCOptions;
import org.videolan.vlc.util.VoiceSearchParams;
import org.videolan.vlc.util.WeakHandler;
import org.videolan.vlc.widget.VLCAppWidgetProvider;
import org.videolan.vlc.widget.VLCAppWidgetProviderBlack;
import org.videolan.vlc.widget.VLCAppWidgetProviderWhite;
import java.io.File;
import java.net.URI;
......@@ -197,6 +200,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
public static final int REPEAT_NONE = 0;
public static final int REPEAT_ONE = 1;
public static final int REPEAT_ALL = 2;
private boolean mHasWidget;
private boolean mShuffling = false;
private int mRepeating = REPEAT_NONE;
private Random mRandom = null; // Used in shuffling process
......@@ -257,6 +261,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
PowerManager pm = (PowerManager) VLCApplication.getAppContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
updateHasWidget();
initMediaSession();
IntentFilter filter = new IntentFilter();
......@@ -271,6 +276,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
filter.addAction(ACTION_REMOTE_LAST_VIDEO_PLAYLIST);
filter.addAction(ACTION_REMOTE_SWITCH_VIDEO);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_INIT);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_ENABLED);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_DISABLED);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
filter.addAction(VLCApplication.SLEEP_INTENT);
......@@ -290,6 +297,12 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
}
private void updateHasWidget() {
AppWidgetManager manager = AppWidgetManager.getInstance(this);
mHasWidget = manager.getAppWidgetIds(new ComponentName(this, VLCAppWidgetProviderWhite.class)).length != 0
|| manager.getAppWidgetIds(new ComponentName(this, VLCAppWidgetProviderBlack.class)).length != 0;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null)
......@@ -473,8 +486,10 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
}
} else if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_INIT)) {
updateWidget();
} else if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_ENABLED)
|| action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_DISABLED)) {
updateHasWidget();
}
/*
* headset plug events
*/
......@@ -783,17 +798,12 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
}
private void executeUpdate() {
executeUpdate(true);
}
private void executeUpdate(Boolean updateWidget) {
synchronized (mCallbacks) {
for (Callback callback : mCallbacks) {
callback.update();
}
}
if (updateWidget)
updateWidget();
updateWidget();
updateMetadata();
broadcastMetadata();
}
......@@ -1483,7 +1493,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
}
private void updateWidget() {
if (!isVideoPlaying()) {
if (mHasWidget && !isVideoPlaying()) {
updateWidgetState();
updateWidgetCover();
}
......@@ -1491,7 +1501,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
private void updateWidgetState() {
final MediaWrapper media = getCurrentMedia();
Intent widgetIntent = new Intent(VLCAppWidgetProvider.ACTION_WIDGET_UPDATE);
final Intent widgetIntent = new Intent(VLCAppWidgetProvider.ACTION_WIDGET_UPDATE);
if (hasCurrentMedia()) {
widgetIntent.putExtra("title", media.getTitle());
widgetIntent.putExtra("artist", media.isArtistUnknown() && media.getNowPlaying() != null ?
......@@ -1511,11 +1521,12 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
}
private void updateWidgetPosition(final float pos) {
if (!mHasWidget)
return;
// no more than one widget mUpdateMeta for each 1/50 of the song
long timestamp = Calendar.getInstance().getTimeInMillis();
if (!hasCurrentMedia() || timestamp - mWidgetPositionTimestamp < getCurrentMedia().getLength() / 50)
return;
updateWidgetState();
mWidgetPositionTimestamp = timestamp;
sendBroadcast(new Intent(VLCAppWidgetProvider.ACTION_WIDGET_UPDATE_POSITION)
.putExtra("position", pos));
......
......@@ -55,6 +55,8 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
public static final String ACTION_WIDGET_UPDATE = ACTION_WIDGET_PREFIX+"UPDATE";
public static final String ACTION_WIDGET_UPDATE_COVER = ACTION_WIDGET_PREFIX+"UPDATE_COVER";
public static final String ACTION_WIDGET_UPDATE_POSITION = ACTION_WIDGET_PREFIX+"UPDATE_POSITION";
public static final String ACTION_WIDGET_ENABLED = ACTION_WIDGET_PREFIX+"ENABLED";
public static final String ACTION_WIDGET_DISABLED = ACTION_WIDGET_PREFIX+"DISABLED";
private static String sCurrentArtworkMrl;
@Override
......@@ -62,19 +64,16 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
super.onUpdate(context, appWidgetManager, appWidgetIds);
/* init widget */
Intent i = new Intent(ACTION_WIDGET_INIT);
onReceive(context, i);
onReceive(context, new Intent(ACTION_WIDGET_INIT));
/* ask a refresh from the service if there is one */
i = new Intent(ACTION_WIDGET_INIT);
i.setPackage(BuildConfig.APPLICATION_ID);
context.sendBroadcast(i);
context.sendBroadcast(new Intent(ACTION_WIDGET_INIT).setPackage(BuildConfig.APPLICATION_ID));
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onReceive(final Context context, final Intent intent) {
String action = intent.getAction();
final String action = intent.getAction();
if (!action.startsWith(ACTION_WIDGET_PREFIX)) {
super.onReceive(context, intent);
return;
......@@ -103,7 +102,7 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
views.setOnClickPendingIntent(R.id.forward, piForward);
views.setOnClickPendingIntent(R.id.cover, piVlc);
if (AndroidUtil.isJellyBeanMR1OrLater && TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL) {
boolean black = this instanceof VLCAppWidgetProviderBlack;
final boolean black = this instanceof VLCAppWidgetProviderBlack;
views.setImageViewResource(R.id.forward, black ? R.drawable.ic_widget_previous_w : R.drawable.ic_widget_previous);
views.setImageViewResource(R.id.backward, black ? R.drawable.ic_widget_next_w : R.drawable.ic_widget_next);
}
......@@ -112,7 +111,7 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
if (ACTION_WIDGET_UPDATE.equals(action)) {
String title = intent.getStringExtra("title");
String artist = intent.getStringExtra("artist");
boolean isplaying = intent.getBooleanExtra("isplaying", false);
final boolean isplaying = intent.getBooleanExtra("isplaying", false);
views.setTextViewText(R.id.songName, title);
views.setTextViewText(R.id.artist, artist);
......@@ -144,13 +143,14 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
views.setProgressBar(R.id.timeline, 100, 0, false);
}
} else if (ACTION_WIDGET_UPDATE_POSITION.equals(action)) {
float pos = intent.getFloatExtra("position", 0f);
final float pos = intent.getFloatExtra("position", 0f);
views.setProgressBar(R.id.timeline, 100, (int) (100 * pos), false);
}
applyUpdate(context, views, partial);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void applyUpdate(Context context, RemoteViews views, boolean partial) {
ComponentName widget = new ComponentName(context, this.getClass());
AppWidgetManager manager = AppWidgetManager.getInstance(context);
......@@ -164,4 +164,15 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
abstract protected int getPlayPauseImage(boolean isPlaying);
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.sendBroadcast(new Intent(ACTION_WIDGET_ENABLED));
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.sendBroadcast(new Intent(ACTION_WIDGET_DISABLED));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment