Commit ec765bf5 authored by Sébastien Toque's avatar Sébastien Toque
Browse files

widget: move updates into the service

parent 80b5a2c5
......@@ -25,10 +25,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.videolan.vlc.android.widget.VLCAppWidgetProvider;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
......@@ -44,6 +48,7 @@ import android.os.Message;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.RemoteViews;
public class AudioService extends Service {
......@@ -79,6 +84,7 @@ public class AudioService extends Service {
mPlayedMedia = new ArrayList<Media>();
mPrevious = new Stack<Media>();
mEventManager = EventManager.getIntance();
updateWidget(this);
}
@Override
......@@ -90,23 +96,48 @@ public class AudioService extends Service {
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_BACKWARD);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_PLAY);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_STOP);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
registerReceiver(headsetReciever, filter);
registerReceiver(serviceReciever, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(headsetReciever);
unregisterReceiver(serviceReciever);
super.onDestroy();
}
private BroadcastReceiver headsetReciever = new BroadcastReceiver() {
private BroadcastReceiver serviceReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int state = intent.getIntExtra("state", 0);
if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_PLAY)) {
if (mLibVLC.isPlaying() && mCurrentMedia != null) {
pause();
} else if (!mLibVLC.isPlaying() && mCurrentMedia != null) {
play();
} else {
Intent iVlc = new Intent(context, MainActivity.class);
iVlc.putExtra(MainActivity.START_FROM_NOTIFICATION, "");
iVlc.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(iVlc);
}
} else if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_BACKWARD)) {
previous();
}
else if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_STOP)) {
stop();
}
else if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD)) {
next();
}
if (mDetectHeadset) {
if (action.equalsIgnoreCase(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
Log.i(TAG, "Headset Removed.");
......@@ -156,6 +187,10 @@ public class AudioService extends Service {
};
private void executeUpdate() {
executeUpdate(true);
}
private void executeUpdate(Boolean updateWidget) {
for (int i = 0; i < mCallback.size(); i++) {
try {
mCallback.get(i).update();
......@@ -163,6 +198,8 @@ public class AudioService extends Service {
e.printStackTrace();
}
}
if (updateWidget)
updateWidget(this);
}
private Handler mHandler = new Handler() {
......@@ -172,7 +209,7 @@ public class AudioService extends Service {
case SHOW_PROGRESS:
int pos = (int) mLibVLC.getTime();
if (mCallback.size() > 0) {
executeUpdate();
executeUpdate(false);
mHandler.removeMessages(SHOW_PROGRESS);
sendEmptyMessageDelayed(SHOW_PROGRESS, 1000 - (pos % 1000));
}
......@@ -476,4 +513,49 @@ public class AudioService extends Service {
}
};
private void updateWidget(Context context)
{
Log.d(TAG, "Updating widget");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.vlcwidget);
if (mCurrentMedia != null) {
views.setTextViewText(R.id.songName, mCurrentMedia.getTitle());
views.setTextViewText(R.id.artist, mCurrentMedia.getArtist());
}
else {
views.setTextViewText(R.id.songName, "VLC mini player");
views.setTextViewText(R.id.artist, "");
}
views.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause : R.drawable.ic_play);
/* commands */
Intent iBackward = new Intent();
iBackward.setAction(VLCAppWidgetProvider.ACTION_WIDGET_BACKWARD);
Intent iPlay = new Intent();
iPlay.setAction(VLCAppWidgetProvider.ACTION_WIDGET_PLAY);
Intent iStop = new Intent();
iStop.setAction(VLCAppWidgetProvider.ACTION_WIDGET_STOP);
Intent iForward = new Intent();
iForward.setAction(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD);
Intent iVlc = new Intent(context, MainActivity.class);
iVlc.putExtra(MainActivity.START_FROM_NOTIFICATION, "");
PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piPlay = PendingIntent.getBroadcast(context, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piStop = PendingIntent.getBroadcast(context, 0, iStop, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piForward = PendingIntent.getBroadcast(context, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piVlc = PendingIntent.getActivity(context, 0, iVlc, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.backward, piBackward);
views.setOnClickPendingIntent(R.id.play_pause, piPlay);
views.setOnClickPendingIntent(R.id.stop, piStop);
views.setOnClickPendingIntent(R.id.forward, piForward);
views.setOnClickPendingIntent(R.id.linearLayout1, piVlc);
/* update widget */
ComponentName widget = new ComponentName(context, VLCAppWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(widget, views);
}
}
......@@ -20,120 +20,24 @@
package org.videolan.vlc.android.widget;
import org.videolan.vlc.android.AudioPlayer;
import org.videolan.vlc.android.AudioServiceController;
import org.videolan.vlc.android.MainActivity;
import org.videolan.vlc.android.R;
import org.videolan.vlc.android.AudioService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class VLCAppWidgetProvider extends AppWidgetProvider {
public static final String TAG = "VLC/VLCAppWidgetProvider";
public static String ACTION_WIDGET_BACKWARD = "ActionReceiverBack";
public static String ACTION_WIDGET_PLAY = "ActionReceiverPlaypause";
public static String ACTION_WIDGET_STOP = "ActionReceiverStop";
public static String ACTION_WIDGET_FORWARD = "ActionReceiverForward";
private RemoteViews views = new RemoteViews("org.videolan.vlc.android", R.layout.vlcwidget);
private AudioServiceController controller = AudioServiceController.getInstance();
@Override
public void onEnabled(Context context) {
AppWidgetManager a = AppWidgetManager.getInstance(context);
this.onUpdate(context, a, a.getAppWidgetIds(new ComponentName(context, org.videolan.vlc.android.widget.VLCAppWidgetProvider.class)));
super.onEnabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
appWidgetManager.updateAppWidget(cn, buildUpdate(context, appWidgetIds));
}
private RemoteViews buildUpdate(Context context, int[] appWidgetIds) {
Intent iBackward = new Intent(context, VLCAppWidgetProvider.class);
iBackward.setAction(ACTION_WIDGET_BACKWARD);
Intent iPlay = new Intent(context, VLCAppWidgetProvider.class);
iPlay.setAction(ACTION_WIDGET_PLAY);
Intent iStop = new Intent(context, VLCAppWidgetProvider.class);
iStop.setAction(ACTION_WIDGET_STOP);
Intent iForward = new Intent(context, VLCAppWidgetProvider.class);
iForward.setAction(ACTION_WIDGET_FORWARD);
PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, 0);
PendingIntent piPlay = PendingIntent.getBroadcast(context, 0, iPlay, 0);
PendingIntent piStop = PendingIntent.getBroadcast(context, 0, iStop, 0);
PendingIntent piForward = PendingIntent.getBroadcast(context, 0, iForward, 0);
views.setOnClickPendingIntent(R.id.backward, piBackward);
views.setOnClickPendingIntent(R.id.play_pause, piPlay);
views.setOnClickPendingIntent(R.id.stop, piStop);
views.setOnClickPendingIntent(R.id.forward, piForward);
public static String ACTION_WIDGET_BACKWARD = "org.videolan.vlc.android.widget.Backward";
public static String ACTION_WIDGET_PLAY = "org.videolan.vlc.android.widget.Play";
public static String ACTION_WIDGET_STOP = "org.videolan.vlc.android.widget.Stop";
public static String ACTION_WIDGET_FORWARD = "org.videolan.vlc.android.widget.Forward";
return views;
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(ACTION_WIDGET_BACKWARD)) {
controller.previous();
} else if(action.equals(ACTION_WIDGET_STOP)) {
controller.stop();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
} else if(action.equals(ACTION_WIDGET_FORWARD)) {
controller.next();
} else if(action.equals(ACTION_WIDGET_PLAY)) {
if(controller.isPlaying()) {
controller.pause();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
} else {
if(!controller.hasMedia()) {
/* Causes a NDK crash, don't un-comment yet
* This is because the LibVLC pointer is not initalised properly */
/*updateTexts();
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
Intent x = new Intent(context, MainActivity.class);
x.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
context.startActivity(x);
return;*/
Toast.makeText(context, "No media loaded, open VLC first", Toast.LENGTH_SHORT).show();
} else {
controller.play();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_pause);
}
}
}
updateTexts();
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
super.onReceive(context, intent);
}
private void updateTexts() {
Log.d(TAG, "Updating texts in widget");
if(controller.hasMedia()) {
views.setTextViewText(R.id.songName, controller.getTitle());
views.setTextViewText(R.id.artist, controller.getArtist());
if(controller.isPlaying()) {
views.setImageViewResource(R.id.play_pause, R.drawable.ic_pause);
} else {
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
}
} else {
views.setTextViewText(R.id.songName, "VLC mini player");
views.setTextViewText(R.id.artist, "");
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(context, AudioService.class));
}
}
Supports Markdown
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